Contents

Swinging Duke
Feedback Button
Left ArrowRight Arrow

The Swing Button API

In Swing, as in other implementations of UI components, a button is a simple atom component that can be equipped with a text label and an image, both of which are optional. When a button is pressed and released, an ActionEvent is sent to all registered ActionListeners. Swing supports several extra features of buttons, including the following:


Swing's Button Class Hierarchy

In the earliest releases of Swing, JButton, JCheckBox, and JRadioButton were all separate classes. As development of Swing moved forward, the JButton inheritance tree was streamlined. In the final release of Swing, four different button classes -- JButton, JToggleButton, JCheckBox, and JRadioButton -- are all descended from an abstract class named AbstractButton. The following diagram shows the current -- and final -- inheritance configuration of Swing's button classes:

Diagram: Button Hierarchy

The reasons for moving to this new system were:


The Button Model Interface

The JButton class implements a button model interface named ButtonModel. The ButtonModel interface is shared by buttons, checkboxes, toggle buttons, and radio buttons. This arrangement is possible because buttons are very UI-centric components. Unlike some components, such as scroll bars and progress bars, buttons don't have a state that can be expressed by integer values that change along a sliding scale. But they do have methods such as isOpaque and isSelected, which describe attributes that can be set to on or off, and they do have lists of interested listeners. (The isSelected state is used by checkboxes and radio buttons.)

These are some of the methods defined in Swing's button model interface:


Buttons and ActionListener Events

Buttons usually work by serving as a source of ActionEvents. By registering itself as an ActionListener on a button, an object can make sure that it is notified when a button is clicked.

The following code fragment shows how a pair of buttons can be created and can be used as a source of action events. In the example, two radio buttons are instantiated and an action listener is then created to listen to the buttons. The radio buttons that have been created are then used to change the look and feel of the application in which they appear.

This example is taken from a sample program named SimpleExample that is provided with Swing. You can find the complet program in Swing's examples folder.

    // Create the buttons.
	JButton button = new JButton("Hello, world");
        button.setKeyAccelerator('h'); 
        //for looks only; button does nada

	JRadioButton basicButton = new JRadioButton(basic);
        basicButton.setKeyAccelerator('b'); 
	basicButton.setActionCommand(basic);
	basicButton.setSelected(true);

	JRadioButton roseButton = new JRadioButton(rose);
        roseButton.setKeyAccelerator('r'); 
	roseButton.setActionCommand(rose);

	// Group the radio buttons.
	ButtonGroup group = new ButtonGroup();
	group.add(basicButton);
	group.add(roseButton);

        // Register a listener for the radio buttons.
	RadioListener myListener = new RadioListener();
	roseButton.addActionListener(myListener);
	basicButton.addActionListener(myListener);

	add(button);
	add(basicButton);
	add(roseButton);
    }

	/** An ActionListener that listens to the radio buttons. */
    class RadioListener implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    String lnfName = null;

	    if (e.getActionCommand() == rose) {
	        lnfName = "com.sun.java.swing.rose.RoseFactory";
	    } else {
	        lnfName = "com.sun.java.swing.basic.BasicFactory";
	    }

            try {
		UIManager.setLookAndFeel(lnfName);
		SwingUtilities.updateComponentTreeUI(frame);
		frame.pack();
            } 
	    catch (Exception exc) {
                System.err.println
				      ("could not load LookAndFeel: " + lnfName);
            }
	    
	}

 


The API

In Swing, the API for buttons has changed from the API used in the 1.1 API. Deprecated convenience methods are provided for backward source compatibility.


Deprecated APIs

 

The following old Button APIs are deprecated in Swing:

getLabel()
setLabel()

 

Arrows





Version 0.5. Last modified 10/1/97.

Copyright © 1995-97 Sun
Microsystems, Inc. All Rights Reserved.


Sun's Home Page