Title: TOPIC 13B
1TOPIC 13B
Window Interfaces Using Swing Objects
- Buttons and Action Listeners
2class JButton
- Provided to create buttons in Java
- To create button
- Same technique as creating Label and Textile
3Methods Provided by the class JButton
4Methods Provided by the class JButton
5(No Transcript)
6Handling an Event
- Action event
- event created when JButton is clicked
- sends a message to another object
- ActionListener
- actionPerformed method
- 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
- In Java, you must register the listener
7Handling An Event
- Each JButton
- Specify corresponding listener object
- i.e. registering the listener
- Define listener methods
- i.e. those methods that will be invoked when the
event is sent to the listener
8Handling 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- Create a class on top of ActionListener so that
the required object can be instantiated. - 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
-
-
-
10The 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- 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)
12Buttons and ActionListeners(RectangleProgram1)
- Basic steps for using a button in a Swing
program - Create a Button object
- Add the Button object to a container
- Create an ActionListener object that has an
actionPerformed method - Register the listener for the Button object
- The following slides show an example of each step.
13Create a Button Object andAdd the Button to a
Container
- JButton stopButton new JButton(Red)
- contentPane.add(stopButton)
String that will appear on the button
JButton is a predefined Swing class for buttons.
This example uses the Flow Layout so the add
method needs only one parameter.
The button will be added to this container.
14Create 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)
. . .
15The 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)) . . .
16Register the Listener for the Button Object
- If a button has no listener registered for it,
there will be no response when the user clicks on
the button. - An example of registering a listener for a
button
JButton stopButton new JButton(Red) stopButto
n.addActionListener(this) contentPane.add(stopBut
ton)
this refers to the object that includes this code
in a method. In this example the object is a
JFrame class that implements ActionListener.