Is there a "Group Box" equivalent in Java Swing?

Thorsten picture Thorsten · Jan 10, 2009 · Viewed 72.1k times · Source

Trying to build a GUI application in Java/Swing. I'm mainly used to "painting" GUIs on the Windows side with tools like VB (or to be more precise, Gupta SQLWindows... wonder how many people know what that is ;-)).

I can't find an equivalent of a Group Box in Swing...

With a group box, you have a square box (usually with a title) around a couple of related widgets. One example is a group box around a few radio buttons (with the title explaining what the radio buttons are about, e.g. Group Box entitled "Sex" with "Male" and "Female" radio buttons).

I've searched around a bit... the only way I found was to add a sub-pane, set the border on the sub-pane and then add all the widgets in the "group" to the sub-pane. Is there a more elegant way to do that?

Answer

David Koelle picture David Koelle · Jan 10, 2009

Create a JPanel, and add your radiobuttons to it. Don't forget to set the layout of the JPanel to something appropriate.

Then call panel.setBorder(BorderFactory.createTitledBorder(name));