What is the best way to put spaces between objects? Can a Swing JSeparator object be an invisible separator?
Asked Answered
C

4

12

I'm trying to put two buttons inside a panel using Swing widgets. Inside the NetBeans IDE, my JSeparator border property is set to (No border) in the properties pane.

Nevertheless a line appears. This is not what I would expect from a separator object. Am I doing something wrong? Coming from a background in Delphi, and C# WinForms, I expect to find some oddities in Swing. But how exactly do you make a transparent gap of a particular size, between two buttons in a panel? Do I have to play with layouts and avoid the JSeparator?

Update: It should be trivial to do this with a layout and without any separator object. So how do you do that? I am looking into the NetBeans layout customizer and properties inspector and finding no way to do it. (Answer: Layouts with Insets, instead of separators.)

Curmudgeon answered 4/6, 2010 at 15:20 Comment(3)
Is "(no border)" null or an object representing an empty border? null is the UI default borderBegot
By the way, if you ever really wanted to do this you'd need to override the JSeparator's look and feel code to paint nothing. The lines aren't the border.Damalas
JSeperator was invented to create visible seperators. You don't want a seperator--you want a separation, which is achievable by one of the many methods below.Thyratron
S
29

You should take a look at the static utility methods on the Box class. They can be used to manufacture fixed struts that act as invisible separators; e.g.

JPanel pnl = new JPanel(new FlowLayout());
pnl.add(new JButton("Hello"));
pnl.add(Box.createHorizontalStrut(10)); // Fixed width invisible separator.
pnl.add(new JButton("Goodbye");

This produces more compact code than creating / configuring a JPanel yourself with appropriate minimum, maximum and preferred dimensions.

Schuss answered 4/6, 2010 at 15:32 Comment(5)
I think that this code, and the JPanel might both be more pain in the long run, as a standard practice, than appropriate use of layouts.Curmudgeon
@Warren P: Depending on the layout manager you use, this code might be an appropriate use of layouts. Different layout managers have very different ways about achieving the same result.Bula
"APpropriate use of layouts" in my case, means "use the netbeans layout manager instead of generating my own code, while I'm still learning netbeans, swing, and brushing up my stale java language knowledge". ;-)Curmudgeon
There's nothing to say that using a horizontal strut isn't an appropriate use of layouts; I have typically used this approach to space out buttons on a JToolBar. For example, in a word processing application it may be common to add a small strut to separate categories of button.Schuss
this is the best answer if you ask meCookgeneral
A
4

JSeparator is meant to be a visible separator between components.

From the javadoc for JSeparator:

JSeparator provides a general purpose component for implementing divider lines - most commonly used as a divider between menu items that breaks them up into logical groupings.

If you want to put a component in between two components that is invisible just use an JPanel instead. Then set the size of the panel with setPreferedSize() and setMin/MaxSize().

Aldose answered 4/6, 2010 at 15:23 Comment(3)
In most environments other than java, the option to make that line go away would have been considered. Odd that it was not considered by these guys.Curmudgeon
On the contrary, empty Box items and Insets are created for just that purpose. It was considered, just not using JSeparatorDamalas
Ah yes. Insets are perfect for my uses, and box and createHorizontalStrut would be enough for probably any other application. Now I am enlightened. thanks.Curmudgeon
W
2

You don't need JSeparator. Most layouts allow you to set gap (space) between compoponents. And Box class can be particularly useful.

Wychelm answered 4/6, 2010 at 15:28 Comment(4)
How can I have a box layout with two buttons in it with at least 10 pixels between each button? I can't seem to get that.Curmudgeon
Box.createHorizontalStrut(10) in between the buttons, in a FlowLayout.Damalas
Cool. The other guy mentioned that, in code, but I was trying to do this from the IDE (netbeans). Turns out you can do this visually in the layout manager by adjusting "insets".Curmudgeon
I will forever be known as "The other guy" :-(Schuss
R
1

Using addSeparator with a value of 1 for height makes it invisible for me, for example:

MyJToolBar.addSeparator(new Dimension(20, 1));
Radarman answered 19/3, 2015 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.