Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add ButtonGroup to JPanel

JPanel.add(ButtonGroup);

Is not working. I MUST add it to a JPanel because I am using tabs. This is getting really frustrating.I hace not found a way yet

like image 789
ntakouris Avatar asked Sep 22 '13 08:09

ntakouris


People also ask

How do I add a ButtonGroup to a JPanel?

As ButtonGroup is not a component, you cannot add your ButtonGroup to a JPanel. Instead add your buttons to your JPanel, e.g: JPanel jPanel = new JPanel(); ButtonGroup group = new ButtonGroup(); btn1 = new JRadioButton("btn1 ");btn1. setSelected(true); btn2 = new JRadioButton("btn2 "); group.

What can you add to a button group?

You'll usually add only radio buttons to a button group, but you can add regular buttons or check boxes, too. Button groups have nothing to do with the visual appearance of the buttons on the screen. Instead, button groups simply provide a logical grouping for the buttons.

What is ButtonGroup?

The ButtonGroup component manages the selected/unselected state for a set of buttons. For the group, the ButtonGroup instance guarantees that only one button can be selected at a time. Initially, all buttons managed by a ButtonGroup instance are unselected.

What is ButtonModel in Java?

public interface ButtonModel extends ItemSelectable. State model for buttons. This model is used for regular buttons, as well as check boxes and radio buttons, which are special kinds of buttons.


2 Answers

As ButtonGroup is not a component, you cannot add your ButtonGroup to a JPanel. Instead add your buttons to your JPanel, e.g:

JPanel jPanel = new JPanel();
ButtonGroup group = new ButtonGroup();
btn1 = new JRadioButton("btn1 ");btn1.setSelected(true);
btn2 = new JRadioButton("btn2 ");
group.add(btn1 );
group.add(btn2 );

jPanel.add(btn1);
jPanel.add(btn2).

Hope it will be useful.

Thanks

like image 98
Manas Mukherjee Avatar answered Sep 28 '22 12:09

Manas Mukherjee


The ButtonGroup class creates only a logical radio button selection group, not a physical one, and is responsible for ensuring that only one button is selected (by deselecting others in the group).

like image 31
FGA Avatar answered Sep 28 '22 11:09

FGA