Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.

Similar presentations


Presentation on theme: "Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects."— Presentation transcript:

1 Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects

2 Chapter 12 2 class JButton l Provided to create buttons in Java l To create button »Same technique as creating Label and Textile

3 Chapter 12 3 Methods Provided by the class JButton

4 Chapter 12 4 Methods Provided by the class JButton

5 Chapter 12 5

6 6 Handling an Event l Action event: » event created when JButton is clicked »sends a message to another object »ActionListener –actionPerformed method l Event listener: »object which receives message when JButton is clicked »performs some action –some method in the listener object is invoked with the event as the argument –happens automatically l In Java, you must register the listener

7 Chapter 12 7 Handling An Event Each JButton »Specify corresponding listener object »i.e. registering the listener l Define listener methods »i.e. those methods that will be invoked when the event is sent to the listener

8 Chapter 12 8 Handling an Event class ActionListener »Handles action event »Part of package java.awt.Event »The class ActionListener is a special type of class (interface) »Must contain actionPerformed method public interface ActionListener { public void actionPerformed(ActionEvent e); }

9 Chapter 12 9 1. Create a class on top of ActionListener so that the required object can be instantiated. 2. This class must contain the necessary code for the method actionPerformed. private class CalculateButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { // code for calculating area & perimeter // & displaying these quantities }

10 Chapter 12 10 The complete code for the CALCULATE BUTTON handler private class CalculateButtonHandler implements ActionListener (1) { public void actionPerformed(ActionEvent e)(2) { double width, length, area, perimeter;(3) length = Double.parseDouble(lenthTF.getText());(4) width = Double.parseDouble(widthTF.getText());(5) area = length * width;(6) perimeter = 2 * (length + width);(7) areaTF.setText(“” + area);(8) perimeterTF.setText(“” + perimeter);(9) }

11 Chapter 12 11 l Must create the listener object in the program. Must associate this handler with the corresponding JButton CalculateButtonHandler cbHandler; cbHandler = new CalculateButtonHandler(); // instantiates the object calculateB.addActionListener(cbHandler);

12 Chapter 12 12 Buttons and ActionListeners (RectangleProgram1) Basic steps for using a button in a Swing program: l Create a Button object l Add the Button object to a container Create an ActionListener object that has an actionPerformed method l Register the listener for the Button object The following slides show an example of each step.

13 Chapter 12 13 Create a Button Object and Add the Button to a Container JButton stopButton = new JButton(“Red”); contentPane.add(stopButton); String that will appear on the button The button will be added to this container. JButton is a predefined Swing class for buttons. This example uses the Flow Layout so the add method needs only one parameter.

14 Chapter 12 14 Create an ActionListener Object Make a class into an ActionListener : Add the phrase implements ActionListener to the beginning of the class definition: Define a method named actionPerformed public class ButtonDemo extends JFrame implements ActionListener {... public void actionPerformed(ActionEvent e) {...

15 Chapter 12 15 The actionPerformed Method An actionPerformed method must have only one parameter The parameter must be of type ActionEvent The parameter can be used to find the command for the ActionEvent : public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(“Red”))... }

16 Chapter 12 16 Register the Listener for the Button Object l If a button has no listener registered for it, there will be no response when the user clicks on the button. l An example of registering a listener for a button: JButton stopButton = new JButton(“Red”); stopButton.addActionListener(this); contentPane.add(stopButton); this refers to the object that includes this code in a method. In this example the object is a JFrame class that implements ActionListener.


Download ppt "Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects."

Similar presentations


Ads by Google