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

Solution 1:

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

Solution 2:

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"

Solution 3:

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.

Solution 4:

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.