Add ButtonGroup to JPanel
Asked Answered
C

2

17
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

Citation answered 22/9, 2013 at 8:28 Comment(3)
docs.oracle.com/javase/tutorial/uiswing/components/…Hove
What do you mean JPanel.add(ButtonGroup)? Can you share more code sample how are you initializing JPanel, buttonGroup etc?Jangro
For better help sooner, post an SSCCE.Psychotherapy
T
39

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

Theis answered 22/9, 2013 at 8:42 Comment(0)
P
2

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

Prowess answered 12/3, 2021 at 15:42 Comment(1)
Welcome to Stack Overflow. Please write your answer in English, as Stack Overflow is an English only site.Lomax

© 2022 - 2024 — McMap. All rights reserved.