
Example 3−1: Cr eating Pull-Down and Pop-Up Menus in Swing (continued)
// A convenience method for creating radio button menu items
public static JMenuItem radioItem(String label, ActionListener listener,
String command, ButtonGroup mutExGroup) {
JMenuItem item = new JRadioButtonMenuItem(label);
item.addActionListener(listener);
item.setActionCommand(command);
mutExGroup.add(item);
return item;
}
// An event listener class used with the menu items created above
// For this demo, it just displays a dialog box when an item is selected
public static class MenuItemActionListener implements ActionListener {
Component parent;
public MenuItemActionListener(Component parent) { this.parent = parent; }
public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem) e.getSource();
String cmd = item.getActionCommand();
JOptionPane.showMessageDialog(parent, cmd + " was selected.");
}
}
}
JTree and TreeModel
The javax.swing.JTree class is a powerful Swing component for displaying tree-
structur ed data. Like all Swing components,
JTree relies on a separate model
object to hold and repr esent the data that it displays. Most Swing components cre-
ate this model object automatically, and you never need to work with it explicitly.
The
JTree component, however, displays data that is much more complex than a
typical Swing component. When you are working with a
JTree, you must create a
model object that implements the ...