![]() ![]() ![]() ![]() |
GUI Changes: The AWT Grows Up |
The Swing release supports radio buttons with theRadioButton
and
ButtonGroup
classes. Because
RadioButton
inherits fromAbstractButton
, Swing radio buttons have all the usual button characteristics, as discussed in How to Use Buttons. For example, you can specify images to be used in radio buttons.Here is a picture of an application that has two radio buttons:
![]()
[We'll try to make the example more interesting...]
Try this:
- Compile and run the application. The source file is
RadioButtonDemo.java
.
See Getting Started with Swing if you need help.- Click Button 2.
Button 2 becomes selected, which makes Button 1 become unselected.- Look at the messages displayed at the standard output.
This application registers a listener for each kind of event a button can send -- action, change, and item. Each time it receives an event, the application displays a message describing the event.- Click Button 2 again, and look at the messages displayed at the standard output.
Often, the only event handler a radio button needs is an action listener. You can use an item listener instead if you're simply monitoring state changes, rather than acting on them. You don't need to implement a change listener unless your program needs to know every time the button's appearance changes. [check all this]
Note: If you have a strong opinion about what events should be generated by radio buttons, please let us know. For example, a radio button currently generates an action event even when the user clicks an already selected button. Perhaps it shouldn't.
Below is the code from
RadioButtonDemo.java
that creates the radio buttons in the previous example and reacts to clicks.The following tables list the commonly used//In initialization code: // Create the buttons. JRadioButton firstButton = new JRadioButton(first); firstButton.setKeyAccelerator('1'); firstButton.setActionCommand(first); firstButton.setSelected(true); JRadioButton secondButton = new JRadioButton(second); secondButton.setKeyAccelerator('2'); secondButton.setActionCommand(second); // Group the radio buttons. ButtonGroup group = new ButtonGroup(); group.add(firstButton); group.add(secondButton); // Register a listener for the radio buttons. RadioListener myListener = new RadioListener(); firstButton.addActionListener(myListener); firstButton.addChangeListener(myListener); firstButton.addItemListener(myListener); secondButton.addActionListener(myListener); secondButton.addChangeListener(myListener); secondButton.addItemListener(myListener); . . . class RadioListener implements ActionListener, //only one event type needed ChangeListener, //for curiosity only ItemListener { //for curiosity only public void actionPerformed(ActionEvent e) { String factoryName = null; System.out.print("ActionEvent received: "); if (e.getActionCommand() == first) { System.out.println(first + " pressed."); } else { System.out.println(second + " pressed."); } } public void itemStateChanged(ItemEvent e) { System.out.println("ItemEvent received: " + e.getItem() + " is now " + ((e.getStateChange() == ItemEvent.SELECTED)? "selected.":"unselected")); } public void stateChanged(ChangeEvent e) { System.out.println("ChangeEvent received from: " + e.getSource()); } }RadioButton
andButtonGroup
methods and constructors. See How to Use Buttons for information on theAbstractButton
API thatRadioButton
inherits.[table to be provided]
![]() ![]() ![]() ![]() |
GUI Changes: The AWT Grows Up |