A button is a component the user clicks to trigger a specific action. A Java application can use several types of buttons, including command buttons, check boxes, toggle buttons and radio buttons. Figure 11.14 shows the inheritance hierarchy of the Swing buttons we cover in this chapter. As you can see, all the button types are subclasses of AbstractButton (package javax.swing), which declares the common features of Swing buttons. In this section, we concentrate on buttons that are typically used to initiate a command.
Figure 11.14. Swing button hierarchy.
Look-and-Feel Observation 11.7
Buttons typically use book-title capitalization. |
A command button (see output of Fig. 11.15) generates an ActionEvent when the user clicks the button. Command buttons are created with class JButton. The text on the face of a JButton is called a button label. A GUI can have many JButtons, but each button label typically should be unique in the portion of the GUI that is currently displayed.
Figure 11.15. Command buttons and action events.
(This item is displayed on page 534 in the print version)
1 // Fig. 11.15: ButtonFrame.java 2 // Creating JButtons. 3 import java.awt.FlowLayout; 4 import java.awt.event.ActionListener; 5 import java.awt.event.ActionEvent; 6 import javax.swing.JFrame; 7 import javax.swing.JButton; 8 import javax.swing.Icon; 9 import javax.swing.ImageIcon; 10 import javax.swing.JOptionPane; 11 12 public class ButtonFrame extends JFrame 13 { 14 private JButton plainJButton; // button with just text 15 private JButton fancyJButton; // button with icons 16 17 // ButtonFrame adds JButtons to JFrame 18 public ButtonFrame() 19 { 20 super( "Testing Buttons" ); 21 setLayout( new FlowLayout() ); // set frame layout 22 23 plainJButton = new JButton( "Plain Button" ); // button with text 24 add( plainJButton ); // add plainJButton to JFrame 25 26 Icon bug1 = new ImageIcon( getClass().getResource( "bug1.gif" ) ); 27 Icon bug2 = new ImageIcon( getClass().getResource( "bug2.gif" ) ); 28 fancyJButton = new JButton( "Fancy Button", bug1 ); // set image 29 fancyJButton.setRolloverIcon( bug2 ); // set rollover image 30 add( fancyJButton ); // add fancyJButton to JFrame 31 32 // create new ButtonHandler for button event handling 33 ButtonHandler handler = new ButtonHandler(); 34 fancyJButton.addActionListener( handler ); 35 plainJButton.addActionListener( handler ); 36 } // end ButtonFrame constructor 37 38 // inner class for button event handling 39 private class ButtonHandler implements ActionListener 40 { 41 // handle button event 42 public void actionPerformed( ActionEvent event ) 43 { 44 JOptionPane.showMessageDialog( ButtonFrame.this, String.format( 45 "You pressed: %s", event.getActionCommand() ) ); 46 } // end method actionPerformed 47 } // end private inner class ButtonHandler 48 } // end class ButtonFrame |
Look-and-Feel Observation 11.8
Having more than one JButton with the same label makes the JButtons ambiguous to the user. Provide a unique label for each button. |
The application of Fig. 11.15 and Fig. 11.16 creates two JButtons and demonstrates that JButtons support the display of Icons. Event handling for the buttons is performed by a single instance of inner class ButtonHandler (lines 3947).
Figure 11.16. Test class for ButtonFrame.
1 // Fig. 11.16: ButtonTest.java 2 // Testing ButtonFrame. 3 import javax.swing.JFrame; 4 5 public class ButtonTest 6 { 7 public static void main( String args[] ) 8 { 9 ButtonFrame buttonFrame = new ButtonFrame(); // create ButtonFrame 10 buttonFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 11 buttonFrame.setSize( 275, 110 ); // set frame size 12 buttonFrame.setVisible( true ); // display frame 13 } // end main 14 } // end class ButtonTest |
Lines 1415 declare JButton variables plainButton and fancyButton. The corresponding objects are instantiated in the constructor. Line 23 creates plainButton with the button label "Plain Button". Line 24 adds the button to the JFrame.
A JButton can display an Icon. To provide the user with an extra level of visual interaction with the GUI, a JButton can also have a rollover Iconan Icon that is displayed when the user positions the mouse over the button. The icon on the button changes as the mouse moves in and out of the button's area on the screen. Lines 2627 create two ImageIcon objects that represent the default Icon and rollover Icon for the JButton created at line 28. Both statements assume that the image files are stored in the same directory as the application (which is commonly the case for applications that use images). These image files have been provided for you.
Line 28 creates fancyButton with the text "Fancy Button" and the icon bug1. By default, the text is displayed to the right of the icon. Line 29 uses setRolloverIcon (inherited from class AbstractButton) to specify the image displayed on the button when the user positions the mouse over it. Line 30 adds the button to the JFrame.
Look-and-Feel Observation 11.9
Because class AbstractButton supports displaying text and images on a button, all subclasses of AbstractButton also support displaying text and images. |
Look-and-Feel Observation 11.10
Using rollover icons for JButtons provides users with visual feedback indicating that when they click the mouse while the cursor is positioned over the button, an action will occur. |
JButtons, like JTextFields, generate ActionEvents that can be processed by any ActionListener object. Lines 3335 create an object of private inner class ButtonHandler and register it as the event handler for each JButton. Class ButtonHandler (lines 3947) declares actionPerformed to display a message dialog box containing the label for the button the user pressed. For a JButton event, ActionEvent method getActionCommand returns the label on the button.
Accessing the this Reference in an Object of a Top-Level Class From an Inner Class
When you execute this application and click one of its buttons, notice that the message dialog that appears is centered over the application's window. This occurs because the call to JOptionPane method showMessageDialog (lines 4445 of Fig. 11.15) uses ButtonFrame.this rather than null as the first argument. When this argument is not null, it represents the so-called parent GUI component of the message dialog (in this case the application window is the parent component) and enables the dialog to be centered over that component when the dialog is displayed. ButtonFrame.this represents the this reference of the object of top-level class ButtonFrame.
Software Engineering Observation 11.4
When used in an inner class, keyword this refers to the current inner-class object being manipulated. An inner-class method can use its outer-class object's this by preceding this with the outer-class name and a dot, as in ButtonFrame.this. |
Introduction to Computers, the Internet and the World Wide Web
Introduction to Java Applications
Introduction to Classes and Objects
Control Statements: Part I
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
GUI Components: Part 1
Graphics and Java 2D™
Exception Handling
Files and Streams
Recursion
Searching and Sorting
Data Structures
Generics
Collections
Introduction to Java Applets
Multimedia: Applets and Applications
GUI Components: Part 2
Multithreading
Networking
Accessing Databases with JDBC
Servlets
JavaServer Pages (JSP)
Formatted Output
Strings, Characters and Regular Expressions
Appendix A. Operator Precedence Chart
Appendix B. ASCII Character Set
Appendix C. Keywords and Reserved Words
Appendix D. Primitive Types
Appendix E. (On CD) Number Systems
Appendix F. (On CD) Unicode®
Appendix G. Using the Java API Documentation
Appendix H. (On CD) Creating Documentation with javadoc
Appendix I. (On CD) Bit Manipulation
Appendix J. (On CD) ATM Case Study Code
Appendix K. (On CD) Labeled break and continue Statements
Appendix L. (On CD) UML 2: Additional Diagram Types
Appendix M. (On CD) Design Patterns
Appendix N. Using the Debugger
Inside Back Cover