What is the best way to put spaces between objects? Can a Swing JSeparator object be an invisible separator?
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));
![Warren P](https://i.stack.imgur.com/aEXJK.png?s=256&g=1)
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, 2022Comments
-
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 about 14 yearsIn 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 about 14 yearsHow 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 about 14 yearsI 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 about 14 years
Box.createHorizontalStrut(10)
in between the buttons, in aFlowLayout
. -
justkt about 14 yearsOn the contrary, empty
Box
items andInsets
are created for just that purpose. It was considered, just not usingJSeparator
-
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 about 14 yearsCool. 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 about 14 yearsAh 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 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 about 14 yearsI will forever be known as "The other guy" :-(
-
Adamski about 14 yearsThere'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 almost 12 yearsthis is the best answer if you ask me