Java Reference
In-Depth Information
Table 6-14. JRadioButtonMenuItem UIResource Elements (Continued)
Property String
Object Type
RadioButtonMenuItem.selectionBackground
Color
RadioButtonMenuItem.selectionForeground
Color
RadioButtonMenuItemUI
String
A Complete JRadioButtonMenuItem Usage Example
To help you understand the JRadioButtonMenuItem usage, the program shown in Listing 6-5
demonstrates how to put everything together, including listening for selection of all the items
on the menu, from either an ActionListener or an ItemListener . The output for the program is
shown in Figure 6-8.
Figure 6-8. JRadioButtonMenuItem usage example output
Listing 6-5. The RadioButtonSample Class Definition
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class RadioButtonSample {
static Icon threeIcon = new ImageIcon("3.gif");
static Icon fourIcon = new ImageIcon("4.gif");
static Icon fiveIcon = new ImageIcon("5.gif");
static Icon sixIcon = new ImageIcon("6.gif");
public static class ButtonActionListener implements ActionListener {
public void actionPerformed (ActionEvent actionEvent) {
AbstractButton aButton = (AbstractButton)actionEvent.getSource();
boolean selected = aButton.getModel().isSelected();
System.out.println (actionEvent.getActionCommand() +
" - selected? " + selected);
}
}
 
Search WWH ::




Custom Search