|
Swing JRadioButton
Part:
1
2
(Continued from previous part...)
This program creates two radio buttons and puts them in a single button group.
Each button has 3 listeners to handle 3 different types of events. A counter is
used in the listener class to help to identify the order of events.
If you run this program, you will see two radio buttons: one labeled as "On" and
the other labeled as "Off". If you press the "On" button and hold it, you will
see 2 messages showing in command window. If you release the "On" button, you
will see 5 more messages. If you continue to press the "Off" button and hold it,
you will see 2 more messages. If you release the "Off" button, you will see 7 more
messages. Here is the list of all the messages:
1: State changed on - On
2: State changed on - On - "On" pressed
3: State changed on - On
4: Item state changed - On
5: State changed on - On
6: Action performed - On
7: State changed on - On - "On" released
8: State changed on - Off
9: State changed on - Off - "Off" pressed
10: State changed on - On
11: Item state changed - On
12: State changed on - Off
13: Item state changed - Off
14: State changed on - Off
15: Action performed - Off
16: State changed on - Off - "Off" released
Note that:
- Action event raised only once when you release a button.
- Change event (stateChanged method call) is raised 2 times when you press a button;
and raised 2 time again when you release a button. This tells us that a button has more than
2 states: selected and deselected.
- Item event (itemStateChanged method call) is raised only once when you release a button.
- In a button group, if one button is selected, other selected buttons will be deselected.
Events #10 and #11 show that when "Off" is selected, "On" is deselected.
Selected Radio Button
If there are many radio buttons in a button group, how do you find the one that is currently
selected? One way is to use the getSelection() method of ButtonGroup class. Here is a sample
program:
/**
* JRadioButtonAction.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JRadioButtonAction implements ActionListener {
ButtonGroup myGroup = null;
JLabel myLebal = null;
public static void main(String[] a) {
JRadioButtonAction myTest = new JRadioButtonAction();
myTest.createFrame();
}
public void createFrame() {
JFrame f = new JFrame("My Radio Buttons");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = f.getContentPane();
c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
myGroup = new ButtonGroup();
JPanel p = new JPanel();
p.setLayout(new GridLayout(3,1));
addOption(p,myGroup,"Red");
addOption(p,myGroup,"Green");
addOption(p,myGroup,"Blue");
c.add(p);
JButton b = new JButton("Select");
b.addActionListener(this);
c.add(b);
myLebal = new JLabel("Please select",SwingConstants.CENTER);
c.add(myLebal);
f.pack();
f.setVisible(true);
}
public void addOption(JPanel p, ButtonGroup g, String t) {
JRadioButton b = new JRadioButton(t);
b.setActionCommand(t);
p.add(b);
g.add(b);
}
public void actionPerformed(ActionEvent e) {
ButtonModel b = myGroup.getSelection();
String t = "Not selected";
if (b!=null) t = b.getActionCommand();
myLebal.setText(t);
}
}
If you run this program, you will see none of the radio buttons is selected initially.
If you click "Select", you will get the "Not selected" message. If you select any of
the radio buttons, then click "Select", you will get the correct message.
Part:
1
2
|