Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unusual gaps while using JSeperator - Java

I have been working on a Swing GUI and getting some unusual and unwanted gaps after adding JSeperator, Any idea how to remove them? Or any other option to how to achieve this nicely!

Visual Description

enter image description here

Gaps are apparent before JLabel "Speed" and after JSlider.

Related Code

control.setLayout(new BoxLayout(control, BoxLayout.X_AXIS));

...another code omitted...

control.add(orientation); //JLabel
control.add(norm); //JRadioButton
control.add(back); //JRadioButton
control.add(new JSeparator(SwingConstants.VERTICAL));
control.add(speedLabel); //JLabel
control.add(speed); //JSlider
control.add(new JSeparator(SwingConstants.VERTICAL));
control.add(turnOutLabel); //JLabel
control.add(right); //JRadioButton
control.add(straight); //JRadioButton
control.add(left); //JRadioButton

What I want is to Have have everything centred and separated by JSeperator,

Visual Description

enter image description here

Thank you.

like image 552
TeaCupApp Avatar asked Sep 22 '11 13:09

TeaCupApp


People also ask

How do I remove unnecessary spaces from a string in Java?

To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

How do I get rid of the white space between words in Java?

The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".

How do you control spacing in Java?

The simplest way to properly space your output in Java is by adding manual spacing. For instance, to output three different integers, "i," "j" and "k," with a space between each integer, use the following code: System. out.


1 Answers

Just replace new JSeparator(...) with the following lines (you can put them in a method if you want):

JSeparator separator = new JSeparator(JSeparator.VERTICAL);
Dimension size = new Dimension(
    separator.getPreferredSize().width,
    separator.getMaximumSize().height);
separator.setMaximumSize(size);

As @kleopatra explained, JSeparator has unbounded maximum size (in both directions), so the trick here is to limit the max width to the preferred width, but still keep the max height unchanged (because the preferred height is 0).

like image 182
jfpoilpret Avatar answered Oct 25 '22 03:10

jfpoilpret