views:

163

answers:

4

I am learning Swing, and finding the behaviour of the JSeparator a bit surprising.

I am 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? alt text

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. [the word I am looking for is... Insets. aha.]

Answer: Use Insets, not JSeparator. If you MUST insert something from the palette, use an empty panel. In netbeans, use Customize Layout, and adjust Inset.

+2  A: 

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().

jjnguy
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.
Warren P
On the contrary, empty `Box` items and `Insets` are created for just that purpose. It was considered, just not using `JSeparator`
justkt
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.
Warren P
+3  A: 

Yes, learning to use layouts effectively is going to be the best solution in the long run.

Mark Peters
I think this is probably going to result in the most understandable and maintainable codebase + forms designs. So how do you do this with a box layout?
Warren P
Try a `GridBagLayout` and set the `Insets` on your `GridBagConstraints`. There's a great tutorial: http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html
justkt
Aha. in netbeans, Customize Layout, and select a button, and then use the group of buttons labelled "Insets". Experiment to find out which of the many [+] and [-] buttons it provides that I need. done.
Warren P
Ah, the NetBeans form builder has a great layout manager set as default. You should be set (but consider learning layout managers for yourself.
justkt
Agreed. Like most Rapid Application Development / Visual Form Builder tools, the learning curve is less steep if I let the NetBeans form builders do the heavy lifting initially, and learn to do the "from code" stuff later.
Warren P
+3  A: 

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

Anton
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.
Warren P
`Box.createHorizontalStrut(10)` in between the buttons, in a `FlowLayout`.
justkt
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".
Warren P
+1. Use Box if you want to think in terms of separations.
Alex Feinman
I will forever be known as "The other guy" :-(
Adamski
+1  A: 

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.

Adamski
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.
Warren P
@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.
Mark Peters
"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". ;-)
Warren P
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.
Adamski