ActionEvents

 

Introduction.  Now that we have developed a couple GUIs that don't actually interact with users you should hopefully be interested in at least some basic ways that a user can invoke program responses to their actions on a GUI.  We will start with the most basic and then add to these later in the course.  Hopefully, by this time you will have an even greater appreciation for the alternatives.

We start with ActionEvents which are monitored through ActionListeners.  The ActionListener interface actually has only one method called actionPerformed( ) which allows the user to determine what action the user took on the GUI for all components that have an associated ActionListener.

ActionListeners can be added to a variety of GUI components such as

  • JButton
  • JTextField
  • JCheckBox
  • JComboBox
  • JRadioButton

Since we only have presented JButton and JTextField at this point in the course, these are the only components that we will have programs monitor for user events.

In this webpage we will again develop GUIs using both an applet and an application.

Our first program is an applet called RepeatTheInput.java.

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RepeatTheInput extends JApplet implements ActionListener
{

// graphical user interface components
JLabel lblGetUserLabel, lblGetUserText, lblDisplayUserLabel;
JTextField txtGetUserLabel, txtGetUserText, txtDisplayUserLabel;
JButton cmdUpdate;
JPanel panAdjust;


public void init( )
{

// setup graphical user interface components
JPanel panAdjust = new JPanel();
panAdjust.setLayout( new FlowLayout() );

lblGetUserLabel = new JLabel( "Enter the label you would like: " );
panAdjust.add( lblGetUserLabel );

txtGetUserLabel = new JTextField( 20 );
panAdjust.add( txtGetUserLabel );

lblGetUserText = new JLabel( "Enter the text you would like: " );
panAdjust.add( lblGetUserText );

txtGetUserText = new JTextField( 20 );
panAdjust.add( txtGetUserText );

lblDisplayUserLabel = new JLabel( "Soon to be changed by you: " );
panAdjust.add( lblDisplayUserLabel );

txtDisplayUserLabel = new JTextField( 20 );
txtDisplayUserLabel.setEditable(false);
panAdjust.add( txtDisplayUserLabel );

cmdUpdate = new JButton( "Update" );
cmdUpdate.addActionListener(this);
panAdjust.add( cmdUpdate );

setContentPane(panAdjust);

} // end method init( )

// the actionEvent handler
public void actionPerformed(ActionEvent userClick)
{

String userLabel;
String userText;
// if statement to determine source
// of user event/click

if (userClick.getSource() == cmdUpdate)
{

// using appropriate get methods
// to obtain user inputs from the GUI

userLabel = txtGetUserLabel.getText( );
userText = txtGetUserText.getText( );

// using appropriate set methods
// to set user inputs into GUI

lblDisplayUserLabel.setText(userLabel);
txtDisplayUserLabel.setText(userText);

} // end if statement

} // end actionPerformed( )

} // end RepeatTheInput class

 

This program does the following
  • the first section of code
    • imports both the swing and awt packages and all their classes
    • imports the event classes in the awt package in order for the program to be able to respond to user inputs
    • extends JApplet
    • implements ActionListener
  • declares the GUI components within the overall inclusive RepeatTheInput class so that they will be available in all other methods in the class
  • within the method init( )
    • initializes a panel to contain the components
    • assigns a FlowLayout to the panel
    • instantiates an object of the JLabel class called lblGetUserLabel
      • its constructor contains a string  "Enter the label you would like: "
    • adds this label to the panel
    • instantiates an object of the JTextField class called txtGetUserLabel
      • its constructor contains a number, 20, for its width
    • adds this text field to the panel
    • instantiates an object of the JLabel class called lblGetUserText
      • its constructor contains a string  "Enter the text you would like: "
    • adds this label to the panel
    • instantiates an object of the JTextField class called txtGetUserText
      • its constructor contains a number, 20, for its width
    • adds this text field to the panel
    • instantiates an object of the JLabel class called lblDisplayUserLabel
      • its constructor contains a string  "Soon to be changed by you: "
    • adds this label to the panel
    • instantiates an object of the JTextField class called txtDisplayUserLabel
      • its constructor contains a number, 20, for its width
      • its editable property is set to false so that user cannot inadvertently enter data
        • by default this causes it to appear in gray rather than white
    • adds this text field to the panel
    • instantiates an object of the JButton class called cmdUpdate
      • its constructor contains a string  "Update"
    • adds the actionListener making use of the this operator
    • adds this button to the panel
    • sets the overall content pane for the applet window to the panel
    • then it ends the method init( )

 

  • now the actionPerformed( ) method is declared and defined
  • String userLabel and userText are local to this method and used to obtain what the user put into the two input text fields
  • an if ( expression ) code segment is used to determine whether the user clicked on the cmdUpdate button - if they did
    • the guiComponent.getText( ) method are used to get the contents of the two JTextFields
      • at this point no error trapping is done
    • the guiComponent.setText( String ) method is used on each of the output components to set their contents

The following contains the HTML file. The size affects the appearance significantly.

 

<html>
<applet code="RepeatTheInput.class" width=450 height=100>
</applet>
</html>

 

When this applet is run you should see the following on your screen.

 

 

Though the text fields will be blank and white.  You should mimic my inputs for your first try.

When you are done, click on the Update button to get the following.

 

 

You should now adjust the inputs and again click the Update button.  Notice how the results adjust without restarting the program.  This is one of the advantages of developing these sorts of forms that contain both the input components and the output components.

Now we will modify this applet to turn it into an application.

An Application Based GUI.  Now I want to run what should seem like almost the same program, but as an application.  We will discuss the code after presenting the code and running it.  You should call the file RepeatTheInputApp.java

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RepeatTheInputApp extends JFrame implements ActionListener
{

// graphical user interface components
JLabel lblGetUserLabel, lblGetUserText, lblDisplayUserLabel;
JTextField txtGetUserLabel, txtGetUserText, txtDisplayUserLabel;
JButton cmdUpdate;
JPanel panAdjust;

// the constructor for the class
public RepeatTheInputApp()
{

super("Repeat the Input");
setSize(450,150);
// setup graphical user interface components
JPanel panAdjust = new JPanel();
panAdjust.setLayout( new FlowLayout() );

lblGetUserLabel = new JLabel( "Enter the label you would like: " );
panAdjust.add( lblGetUserLabel );

txtGetUserLabel = new JTextField( 20 );
panAdjust.add( txtGetUserLabel );

lblGetUserText = new JLabel( "Enter the text you would like: " );
panAdjust.add( lblGetUserText );

txtGetUserText = new JTextField( 20 );
panAdjust.add( txtGetUserText );

lblDisplayUserLabel = new JLabel( "Soon to be changed by you: " );
panAdjust.add( lblDisplayUserLabel );

txtDisplayUserLabel = new JTextField( 20 );
txtDisplayUserLabel.setEditable(false);
panAdjust.add( txtDisplayUserLabel );

cmdUpdate = new JButton( "Update" );
cmdUpdate.addActionListener(this);
panAdjust.add( cmdUpdate );

setContentPane(panAdjust);

} // end method RepeatTheInputApp( )

// the actionEvent handler
public void actionPerformed(ActionEvent userClick)
{

String userLabel;
String userText;
// if statement to determine source
// of user event/click

if (userClick.getSource() == cmdUpdate)
{

// using appropriate get methods
// to obtain user inputs from the GUI

userLabel = txtGetUserLabel.getText();
userText = txtGetUserText.getText();

// using appropriate set methods
// to set user inputs into GUI

lblDisplayUserLabel.setText(userLabel);
txtDisplayUserLabel.setText(userText);

} // end if statement

} // end actionPerformed( )

public static void main(String args[])
{
JFrame frame = new RepeatTheInputApp( );
ExitWindow exit = new ExitWindow( );
frame.addWindowListener(exit);
frame.show( );
} // end main( )

} // end RepeatTheInputApp class

class ExitWindow extends WindowAdapter
{

public void windowClosing(WindowEvent e)
{

System.exit(0);

} // end windowClosing( )

} // end class ExitWindow

 

The discussion of the code follows.  Make sure to focus on the differences between the applet and the application.

  • imports both the swing and awt packages and all their classes but also imports the awt.event classes
    • the awt.event classes need to be imported because
      • we need to have the form listen for someone clicking on the little x that tells the program to close
      • we need the code that should execute when the user clicks on the close button
      • these are the sorts of things that are automatically available when working with applets
      • we need to be able to have the program listen for the user to click on the cmdUpdate button
  • extends JFrame
  • declares the GUI components within the overall inclusive RepeatTheInputApp class so that they will be available in all other methods in the class
  • within the method RepeatTheInputApp( ), which is actually the constructor for the overall inclusive class and wasn't needed in the applet and which must have the same name as its overall inclusive class
    • sets the title
    • sets the size
    • initializes a panel to contain the components
    • assigns a FlowLayout to the panel
    • instantiates an object of the JLabel class called lblGetUserLabel
      • its constructor contains a string  "Enter the label you would like: "
    • adds this label to the panel
    • instantiates an object of the JTextField class called txtGetUserLabel
      • its constructor contains a number, 20, for its width
    • adds this text field to the panel
    • instantiates an object of the JLabel class called lblGetUserText
      • its constructor contains a string  "Enter the text you would like: "
    • adds this label to the panel
    • instantiates an object of the JTextField class called txtGetUserText
      • its constructor contains a number, 20, for its width
    • adds this text field to the panel
    • instantiates an object of the JLabel class called lblDisplayUserLabel
      • its constructor contains a string  "Soon to be changed by you: "
    • adds this label to the panel
    • instantiates an object of the JTextField class called txtDisplayUserLabel
      • its constructor contains a number, 20, for its width
      • its editable property is set to false so that user cannot inadvertently enter data
        • by default this causes it to appear in gray rather than white
    • adds this text field to the panel
    • instantiates an object of the JButton class called cmdUpdate
      • its constructor contains a string  "Update"
    • adds the actionListener making use of the this operator
    • adds this button to the panel
    • sets the overall content pane for the applet window to the panel
    • end this RepeatTheInputApp( ) constructor
  • within the method main( ) which wasn't needed in the applet
    • instantiate/construct a JFrame object of type RepeatTheInputApp( )
    • instantiate/construct an ExitWindow object called exit
    • add the capability for the application to listen for the user to click on the overall frame based on the object exit of the ExitWindow class
    • cause the frame to display using the show( ) method from the JFrame class
    • end the method main( )
  • end the overall inclusive class RepeatTheInputApp

 

  • start a new class to deal with the user trying to close the frame/window called ExitWindow that extends the java awt.event class WindowAdapter
  • create a method called windowClosing( ) that responds to the user clicking on the close button on the frame
    • within this method we tell the program to System.exit(0) when the user clicks on the  close button

Notice how when you compile the RepeatTheInputApp program, you get a class called RepeatTheInputApp.class but you also get a class called ExitWindow.class.

When you run the program the form will not look much different than the one for the applet.

 

 

Pressing the update button results in the following.

 

 

This GUI is quite stable, particularly since it accepts string inputs.  But you can keep modifying the contents without restarting the program.