MustSeeMelons
MustSeeMelons

Reputation: 756

How to add borders to components when using GroupLayout?

For example, I have two radio buttons and I want a border around them so that the UI looks clearer? Tried searching but didn't find anything useful. Thanks!

Upvotes: 1

Views: 2145

Answers (2)

Mady
Mady

Reputation: 653

Create a panel, put those radio buttons in that panel.. and create a border around the panel

How to use borders

Upvotes: 3

John Snow
John Snow

Reputation: 5344

Below would create a border around your radio buttons. You can create different type of borders, check the BorderFactory API for a description about the different borders

JRadioButton yesButton   = new JRadioButton("Yes", true);
JRadioButton noButton    = new JRadioButton("No", false);

ButtonGroup bgroup = new ButtonGroup();
bgroup.add(yesButton);
bgroup.add(noButton);

JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(2, 1));
radioPanel.add(yesButton);
radioPanel.add(noButton);

radioPanel.setBorder(BorderFactory.createTitledBorder(
           BorderFactory.createEtchedBorder(), "BorderTitle"));

Upvotes: 4

Related Questions