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
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
Thank you.
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.
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 "".
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.
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With