|
|
< Day Day Up > |
|
How to Use ButtonsTo create a button, you can instantiate one of the many classes that descend from the AbstractButton[6] class. Table 2 shows the Swing-defined AbstractButton subclasses that you might want to use.
Note: If you want to collect a group of buttons into a row or column, then you should check out tool bars. You can read about them in How to Use Tool Bars (page 427) later in this chapter. First, this section explains the basic button API that AbstractButton defines—and thus what all Swing buttons have in common. Next, it describes the small amount of API that JButton adds to AbstractButton. After that, this section shows you how to use specialized API to implement check boxes and radio buttons. How to Use the Common Button APIFigure 2 shows a picture of an application that displays three buttons. Figure 2. The ButtonDemo application.
Try This:
As the ButtonDemo example shows, a Swing button can display both text and an image. In ButtonDemo, each button has its text in a different place, relative to its image. The underlined letter in each button's text shows the button's mnemonic—the keyboard alternative. In most look and feels, the user can click a button by pressing the Alt key and the mnemonic. For example, Alt-M clicks the middle button in ButtonDemo. When a button is disabled, the look and feel automatically generates the button's disabled appearance. However, you could provide an image to substitute for the normal image—for example, gray versions of the images used in the left and right buttons. How you implement event handling depends on the type of button you use and how you use it. Generally, you implement an action listener, which is notified every time the user clicks the button. The code that follows is from ButtonDemo.java. It creates the buttons in the previous example and reacts to button clicks. The bold code is what would remain if the buttons had no images. //In initialization code: ImageIcon leftButtonIcon = createImageIcon("images/right.gif"); ImageIcon middleButtonIcon = createImageIcon("images/middle.gif"); ImageIcon rightButtonIcon = createImageIcon("images/left.gif"); b1 = new JButton("Disable middle button", leftButtonIcon); b1.setVerticalTextPosition(AbstractButton.CENTER); b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales b1.setMnemonic(KeyEvent.VK_D); b1.setActionCommand("disable"); b2 = new JButton("Middle button", middleButtonIcon); b2.setVerticalTextPosition(AbstractButton.BOTTOM); b2.setHorizontalTextPosition(AbstractButton.CENTER); b2.setMnemonic(KeyEvent.VK_M); b3 = new JButton("Enable middle button", rightButtonIcon); //Use the default text position of CENTER, TRAILING (RIGHT). b3.setMnemonic(KeyEvent.VK_E); b3.setActionCommand("enable"); b3.setEnabled(false); //Listen for actions on buttons 1 and 3. b1.addActionListener(this); b3.addActionListener(this); b1.setToolTipText("Click this button to disable " + "the middle button."); b2.setToolTipText("This middle button does nothing " + "when you click it."); b3.setToolTipText("Click this button to enable the " + "middle button."); ... } public void actionPerformed(ActionEvent e) { if ("disable".equals(e.getActionCommand())) { b2.setEnabled(false); b1.setEnabled(false); b3.setEnabled(true); } else { b2.setEnabled(true); b1.setEnabled(true); b3.setEnabled(false); } } protected static ImageIcon createImageIcon(String path) { java.net.URL imgURL = ButtonDemo.class.getResource(path); ...//error handling omitted for clarity... return new ImageIcon(imgURL); } How to Use JButton FeaturesOrdinary buttons—JButton[8] objects—have just a bit more functionality than the AbstractButton class provides: You can make a JButton the default button.
At most one button in a top-level container can be the default button. The default button typically has a highlighted appearance and acts clicked whenever the top-level container has the keyboard focus and the user presses the Enter key. Figure 3 is a picture of a dialog in which the Set button is the default button. Figure 3. The ListDialog component.
You set the default button by invoking the setDefaultButton method on a top-level container's root pane. Here's the code that sets up the default button for the ListDialog example:
//In the constructor for a JDialog subclass:
getRootPane().setDefaultButton(setButton);
The exact implementation of the default button feature depends on the look and feel. For example, in the Windows look and feel, the default button changes to whichever button has the focus so that pressing Enter clicks the focused button. When no button has the focus, the button you originally specified as the default button becomes the default button again. The Button APITables 3 through 5 list the commonly used button-related API. With the exception of the JButton constructors, all of the listed API is defined by the AbstractButton class. Other methods you might call, such as setFont and setForeground, are listed in the API tables in the section The JComponent Class (page 53) in Chapter 3. You should also refer to the API documentation for AbstractButton[9] and JButton.[10]
Examples That Use ButtonsThe following table lists some of the many examples that use buttons. Also see Examples That Use Tool Bars (page 433), which lists programs that add JButton objects to JToolBars, Examples That Use Check Boxes (page 166), and Examples That Use Radio Buttons (page 315).
|
|
|
< Day Day Up > |
|