61

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?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Thorsten
  • 12,921
  • 17
  • 60
  • 79
  • Perhaps you could explain what a group box is. – Geo Jan 10 '09 at 20:20
  • I think he means the control group you see in many dialog boxes, where you have a square around a bunch of widgets such as radio buttons, for example. – Uri Jan 10 '09 at 20:23

7 Answers7

120

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

David Koelle
  • 20,726
  • 23
  • 93
  • 130
8

Others have already commetned about JPanel and using a TitledBorder, that's fine.

However, when playing with Swing LayoutManagers, you may find it annoying that components in different JPanels cannot align correctly (each panel has its own LayoutManager).

For this reason, it is a good practice (check "JGoodies" on the web for more details) in Swing GUIs to NOT use TitledBorders but rather separate groups of components in a JPanel by a JLabel followed by a horizontal JSeparator.

Ref. "First Aid for Swing"

jfpoilpret
  • 10,449
  • 2
  • 28
  • 32
  • 3
    Is there Oracle or system specific guidelines that support this statement? That label + separator should be preferred over group boxing? JGoodies is just *one* company implementing Java. – Kissaki Sep 15 '14 at 14:42
  • 1
    @Kissaki I mentioned it was a good practice, not an official guideline from anyone. The fact is, with Swing layouts managers system, you CANNOT align components across 2 panels. Hence using borders means losing alignment of wour components. – jfpoilpret Oct 29 '16 at 08:21
4

A Group box is just a set of 'logically grouped widgets'. This in the swing world is a JPanel.

Add your widgets to a JPanel.

Set its border type to 'Titled Border' and give the title, same as the name of the VB6 'frame'.

Voila. You have your group box.

Nivas
  • 18,126
  • 4
  • 62
  • 76
2

Here's a quote from the JRadioButton javadocs since you brought up radio buttons.

An implementation of a radio button -- an item that can be selected or deselected, and which displays its state to the user. Used with a ButtonGroup object to create a group of buttons in which only one button at a time can be selected. (Create a ButtonGroup object and use its add method to include the JRadioButton objects in the group.)

Note: The ButtonGroup object is a logical grouping -- not a physical grouping. To create a button panel, you should still create a JPanel or similar container-object and add a Border to it to set it off from surrounding components.

Community
  • 1
  • 1
Bill
  • 34,524
  • 1
  • 17
  • 6
1

Not AFAIK, at least not with standard swing widgets.

In VB you have a group widget, which is essentially a panel + border.

In Swing you have a JPanel which is the container widget, and you create and set a border object on it only if you need one. One can argue that in a way that is more elegant since you don't pay for something you don't use (e.g., border)

Uri
  • 88,451
  • 51
  • 221
  • 321
1

As David Koelle mentioned about setting up border through java code, you can also achieve similar result in designer mode.

enter image description here

Gaurang
  • 67
  • 8
-1

I'm responding based on the Uri's comment which explaind what the OP meant by Group Box:

Uri: I think he means the control group you see in many dialog boxes, where you have a square around a bunch of widgets such as radio buttons, for example.

As far as I know, every JComponent can set a border for itself, so you don't need a second panel.

Troyseph
  • 4,960
  • 3
  • 38
  • 61
Geo
  • 93,257
  • 117
  • 344
  • 520