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

Warren P
  • 65,725
  • 40
  • 181
  • 316
  • Is "(no border)" null or an object representing an empty border? `null` is the UI default border – Pete Kirkham Jun 04 '10 at 15:28
  • 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. – justkt Jun 04 '10 at 15:54
  • 1
    JSeperator was invented to create visible seperators. You don't want a seperator--you want a separa*tion*, which is achievable by one of the many methods below. – Alex Feinman Jun 04 '10 at 16:23

4 Answers4

28

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
  • 54,009
  • 15
  • 113
  • 152
  • 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 Jun 04 '10 at 15:44
  • 2
    @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 Jun 04 '10 at 15:48
  • "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 Jun 04 '10 at 15:57
  • 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 Jun 04 '10 at 16:50
  • this is the best answer if you ask me – Epaga Aug 13 '12 at 13:15
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().

jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • 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 Jun 04 '10 at 15:38
  • 1
    On the contrary, empty `Box` items and `Insets` are created for just that purpose. It was considered, just not using `JSeparator` – justkt Jun 04 '10 at 15:46
  • 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 Jun 04 '10 at 15:50
2

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

Anton
  • 2,653
  • 1
  • 16
  • 7
  • 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 Jun 04 '10 at 15:41
  • 1
    `Box.createHorizontalStrut(10)` in between the buttons, in a `FlowLayout`. – justkt Jun 04 '10 at 15:45
  • 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 Jun 04 '10 at 15:49
  • 3
    I will forever be known as "The other guy" :-( – Adamski Jun 04 '10 at 16:49
1

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

MyJToolBar.addSeparator(new Dimension(20, 1));
SubJunk
  • 150
  • 8