Reputation: 13
I have created several radio buttons but for some reason I can only select one and if I select another the previously selected radio button will suddenly become unchecked.
code:
package demo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
public class example {
protected Shell shell;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
example window = new example();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
Button btnRadioButton = new Button(shell, SWT.RADIO);
btnRadioButton.setBounds(83, 10, 90, 16);
btnRadioButton.setText("Radio Button");
Button btnRadioButton_1 = new Button(shell, SWT.RADIO);
btnRadioButton_1.setBounds(55, 86, 90, 16);
btnRadioButton_1.setText("Radio Button");
Button btnRadioButton_2 = new Button(shell, SWT.RADIO);
btnRadioButton_2.setBounds(179, 158, 90, 16);
btnRadioButton_2.setText("Radio Button");
Button btnRadioButton_3 = new Button(shell, SWT.RADIO);
btnRadioButton_3.setBounds(293, 65, 90, 16);
btnRadioButton_3.setText("Radio Button");
Button button = new Button(shell, SWT.RADIO);
button.setText("Radio Button");
button.setBounds(303, 103, 90, 16);
Button button_1 = new Button(shell, SWT.RADIO);
button_1.setText("Radio Button");
button_1.setBounds(189, 196, 90, 16);
}
}
I want radio buttons 1,2 and 3 to be linked so only one of these can be selected simultaneously. But I'd like 4,5 and 6 to be in a separate group etc.
How can I fix this, thanks?
Example of use:
Answer Question one using radio buttons 1, 2 and 3
Answer Question two using radio buttons 4, 5 and 6
etc.
Upvotes: 1
Views: 1103
Reputation: 2021
In SWT, you should create the buttons in a Composite to form a group. All 6 buttons are being created in the same composite (shell), so they all are in the same group.
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
Button btnRadioButton = new Button(shell, SWT.RADIO);
btnRadioButton.setBounds(0, 10, 90, 16);
btnRadioButton.setText("Radio Button");
Button btnRadioButton_1 = new Button(shell, SWT.RADIO);
btnRadioButton_1.setBounds(0, 30, 90, 16);
btnRadioButton_1.setText("Radio Button");
Button btnRadioButton_2 = new Button(shell, SWT.RADIO);
btnRadioButton_2.setBounds(0, 50, 90, 16);
btnRadioButton_2.setText("Radio Button");
Composite composite = new Composite(shell, SWT.NULL);
composite.setBounds(0, 70, 300, 200);
composite.setLayout(new RowLayout());
Button btnRadioButton_3 = new Button(composite, SWT.RADIO);
btnRadioButton_3.setBounds(0, 0, 90, 16);
btnRadioButton_3.setText("Radio Button");
Button button = new Button(composite, SWT.RADIO);
button.setText("Radio Button");
button.setBounds(0, 20, 90, 16);
Button button_1 = new Button(composite, SWT.RADIO);
button_1.setText("Radio Button");
button_1.setBounds(0, 40, 90, 16);
Upvotes: 1
Reputation: 37604
You have to use a CheckBoxGroup.
The CheckboxGroup class is used to group together a set of Checkbox buttons. Exactly one check box button in a CheckboxGroup can be in the "on" state at any given time. Pushing any button sets its state to "on" and forces any other button that is in the "on" state into the "off" state.
The following code example produces a new check box group, with three check boxes:
Example for awt:
CheckboxGroup cbg = new CheckboxGroup();
add(new Checkbox("one", cbg, true));
add(new Checkbox("two", cbg, false));
add(new Checkbox("three", cbg, false));
For Swing you have to use ButtonGroup
//In initialization code:
//Create the radio buttons.
JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setMnemonic(KeyEvent.VK_B);
birdButton.setActionCommand(birdString);
birdButton.setSelected(true);
JRadioButton catButton = new JRadioButton(catString);
catButton.setMnemonic(KeyEvent.VK_C);
catButton.setActionCommand(catString);
JRadioButton dogButton = new JRadioButton(dogString);
dogButton.setMnemonic(KeyEvent.VK_D);
dogButton.setActionCommand(dogString);
JRadioButton rabbitButton = new JRadioButton(rabbitString);
rabbitButton.setMnemonic(KeyEvent.VK_R);
rabbitButton.setActionCommand(rabbitString);
JRadioButton pigButton = new JRadioButton(pigString);
pigButton.setMnemonic(KeyEvent.VK_P);
pigButton.setActionCommand(pigString);
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);
group.add(dogButton);
group.add(rabbitButton);
group.add(pigButton);
And finally for SWT you can use the Composite
public class RadioButtonComposite {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Composite composite = new Composite(shell, SWT.NULL);
composite.setLayout(new RowLayout());
Button mrButton = new Button(composite, SWT.RADIO);
mrButton.setText("Mr.");
Button mrsButton = new Button(composite, SWT.RADIO);
mrsButton.setText("Mrs.");
Button msButton = new Button(composite, SWT.RADIO);
msButton.setText("Ms.");
Button drButton = new Button(composite, SWT.RADIO);
drButton.setText("Dr.");
shell.open();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
}
Upvotes: 0