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

20,949

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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

MyJToolBar.addSeparator(new Dimension(20, 1));
Share:
20,949
Warren  P
Author by

Warren P

Software Developer: Web, Desktop, and Server service developer JavaScript (ExtJS/SenchaTouch/jquery), HTML5, CSS C#/ASP.NET MVC/ASP.NET Core/Roslyn/Visual Studio 2015 Delphi/ObjectPascal C (Linux and others) and Objective-C (have an app in the app store) Python C++ Smalltalk/Pharo/Squeak Embedded Systems, and cross-platform stuff (Windows, Linux, Mac OS X) नमस्ते. मैं हिंदी फिल्मों से प्यार है. Bits of code at Bitbucket hosting: https://bitbucket.org/wpostma More bits of code at Github: https://github.com/wpostma

Updated on July 05, 2022

Comments

  • Warren  P
    Warren P almost 2 years

    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
    Warren P about 14 years
    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
    Warren P about 14 years
    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
    Warren P about 14 years
    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.
  • justkt
    justkt about 14 years
    Box.createHorizontalStrut(10) in between the buttons, in a FlowLayout.
  • justkt
    justkt about 14 years
    On the contrary, empty Box items and Insets are created for just that purpose. It was considered, just not using JSeparator
  • Mark Peters
    Mark Peters about 14 years
    @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.
  • Warren  P
    Warren P about 14 years
    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
    Warren P about 14 years
    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
    Warren P about 14 years
    "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". ;-)
  • Adamski
    Adamski about 14 years
    I will forever be known as "The other guy" :-(
  • Adamski
    Adamski about 14 years
    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.
  • Epaga
    Epaga almost 12 years
    this is the best answer if you ask me