Home | | Web Programming | Using Buttons - AWT Controls

Chapter: Java The Complete Reference : The Java Library : Using AWT Controls, Layout Managers, and Menus

Using Buttons - AWT Controls

Perhaps the most widely used control is the push button. A push button is a component that contains a label and that generates an event when it is pressed. Push buttons are objects of type Button.

Using Buttons

 

Perhaps the most widely used control is the push button. A push button is a component that contains a label and that generates an event when it is pressed. Push buttons are objects of type Button. Button defines these two constructors:

 

Button( ) throws HeadlessException Button(String str) throws HeadlessException

 

The first version creates an empty button. The second creates a button that contains str as a label.

 

After a button has been created, you can set its label by calling setLabel( ). You can retrieve its label by calling getLabel( ). These methods are as follows:

 

void setLabel(String str) String getLabel( )

 

Here, str becomes the new label for the button.

 

Handling Buttons

 

Each time a button is pressed, an action event is generated. This is sent to any listeners that previously registered an interest in receiving action event notifications from that component. Each listener implements the ActionListener interface. That interface defines the actionPerformed( ) method, which is called when an event occurs. An ActionEvent object is supplied as the argument to this method. It contains both a reference to the button that generated the event and a reference to the action command string associated with the button. By default, the action command string is the label of the button. Either the button reference or the action command string can be used to identify the button. (You will soon see examples of each approach.)

Here is an example that creates three buttons labeled "Yes", "No", and "Undecided". Each time one is pressed, a message is displayed that reports which button has been pressed. In this version, the action command of the button (which, by default, is its label) is used to determine which button has been pressed. The label is obtained by calling the getActionCommand( ) method on the ActionEvent object passed to actionPerformed( ).

 

// Demonstrate Buttons

import java.awt.*; import java.awt.event.*; import java.applet.*; /*

 

<applet code="ButtonDemo" width=250 height=150> </applet>

 

*/

 

public class ButtonDemo extends Applet implements ActionListener { String msg = "";

 

Button yes, no, maybe;

 

public void init() {

 

yes = new Button("Yes"); no = new Button("No");

 

maybe = new Button("Undecided");

 

add(yes);

 

add(no);

 

add(maybe);

 

yes.addActionListener(this);

 

no.addActionListener(this);

 

maybe.addActionListener(this);

 

}

 

public void actionPerformed(ActionEvent ae) { String str = ae.getActionCommand();

 

if(str.equals("Yes")) {

 

msg = "You pressed Yes.";

 

}

 

else if(str.equals("No")) { msg = "You pressed No.";

}

 

else {

 

msg = "You pressed Undecided.";

 

}

 

repaint();

 

}

 

public void paint(Graphics g) { g.drawString(msg, 6, 100);

}

 

}



Figure 26-1   Sample output from the ButtonDemo applet

 

Sample output from the ButtonDemo program is shown in Figure 26-1.

 

As mentioned, in addition to comparing button action command strings, you can also determine which button has been pressed by comparing the object obtained from the getSource( ) method to the button objects that you added to the window. To do this, you must keep a list of the objects when they are added. The following applet shows this approach:

 

 

// Recognize Button objects.

import java.awt.*;

 

import java.awt.event.*; import java.applet.*;

 

/*

 

<applet code="ButtonList" width=250 height=150> </applet>

 

*/

 

public class ButtonList extends Applet implements ActionListener { String msg = "";

 

Button bList[] = new Button[3];

 

public void init() {

 

Button yes = new Button("Yes"); Button no = new Button("No");

Button maybe = new Button("Undecided");

 

     store references to buttons as added bList[0] = (Button) add(yes);

 

bList[1] = (Button) add(no); bList[2] = (Button) add(maybe);

 

     register to receive action events for(int i = 0; i < 3; i++) {

 

bList[i].addActionListener(this);

 

}

 

}

public void actionPerformed(ActionEvent ae) { for(int i = 0; i < 3; i++) {

 

if(ae.getSource() == bList[i]) {

 

msg = "You pressed " + bList[i].getLabel();

 

}

 

}

 

repaint();

 

}

 

public void paint(Graphics g) { g.drawString(msg, 6, 100);

}

 

}

 

In this version, the program stores each button reference in an array when the buttons are added to the applet window. (Recall that the add( ) method returns a reference to the button when it is added.) Inside actionPerformed( ), this array is then used to determine which button has been pressed.

For simple programs, it is usually easier to recognize buttons by their labels. However, in situations in which you will be changing the label inside a button during the execution of your program, or using buttons that have the same label, it may be easier to determine which button has been pushed by using its object reference. It is also possible to set the action command string associated with a button to something other than its label by calling setActionCommand( ). This method changes the action command string, but does not affect the string used to label the button. Thus, setting the action command enables the action command and the label of a button to differ.

 

In some cases, you can handle the action events generated by a button (or some other type of control) by use of an anonymous inner class (as described in Chapter 24) or a lambda expression (discussed in Chapter 15). For example, assuming the previous programs, here is a set of action event handlers that use lambda expressions:

 

// Use lambda expressions to handle action events.

yes.addActionListener((ae) -> {

 

msg = "You pressed " + ae.getActionCommand(); repaint();

 

});

 

no.addActionListener((ae) -> {

 

msg = "You pressed " + ae.getActionCommand(); repaint();

 

});

 

maybe.addActionListener((ae) -> {

 

msg = "You pressed " + ae.getActionCommand(); repaint();

 

});

 

This code works because ActionListener defines a functional interface, which is an interface with exactly one abstract method. Thus, it can be used by a lambda expression. In general, you can use a lambda expression to handle an AWT event when its listener defines a functional interface. For example, ItemListener is also a functional interface. Of course, whether you use the traditional approach, an anonymous inner class, or a lambda expression will be determined by the precise nature of your application. The remaining examples in this chapter use the traditional approach to event handling so that they can be compiled by nearly any version of Java. However, you might find it interesting to try converting the event handlers to lambda expressions or anonymous inner classes, where appropriate.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Library : Using AWT Controls, Layout Managers, and Menus : Using Buttons - AWT Controls |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.