Multiple ActionEvents

 

Introduction.  Our last programs had only a single component that could cause the program to execute, even though they could then get values from other sources.  In this example we will have more than one command button to give the user some options. 

This applet will allow the user to change the color of a text field based on which command button they select.  This program should be called MultipleChoice.java.

 

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

public class MultipleChoice extends JApplet implements ActionListener
{

// graphical user interface components
JLabel lblInstructions;
JButton cmdRed, cmdGreen, cmdBlue;
JPanel panChange;
JTextField colorIt;

public void init( )
{

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

lblInstructions = new JLabel( "Click on a button to change the color" );
panChange.add( lblInstructions );

// developing the command buttons
cmdRed = new JButton( "Red" );
cmdRed.addActionListener(this);
panChange.add( cmdRed );

cmdGreen = new JButton( "Green" );
cmdGreen.addActionListener(this);
panChange.add( cmdGreen );

cmdBlue = new JButton( "Blue" );
cmdBlue.addActionListener(this);
panChange.add( cmdBlue );

colorIt = new JTextField(20);
panChange.add(colorIt);

setContentPane(panChange);

} // end method init( )

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

// decision block to determine source
// of user event/click

if (userClick.getSource( ) == cmdRed)

colorIt.setBackground(Color.red);

else if (userClick.getSource( ) == cmdGreen)

colorIt.setBackground(Color.green);

else if (userClick.getSource( ) == cmdBlue)

colorIt.setBackground(Color.blue);

// end if / else if block

} // end actionPerformed( )

} // end MultipleChoice 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 MultipleChoice class so that they will be available in all other methods in the class
  • within the method init( )
    • initializes a panel called panChange to contain the components
    • assigns a FlowLayout to the panel
    • instantiates an object of the JLabel class called lblInstructions
      • its constructor contains a string  "Click on a button to change the color "
    • adds this label to the panel
    • instantiates an object of the JButton class called cmdRed
      • its constructor contains a string  "Red"
    • adds the actionListener making use of the this operator
    • adds this button to the panel
    • instantiates an object of the JButton class called cmdGreen
      • its constructor contains a string  "Green"
    • adds the actionListener making use of the this operator
    • adds this button to the panel
    • instantiates an object of the JButton class called cmdBlue
      • its constructor contains a string  "Blue"
    • adds the actionListener making use of the this operator
    • adds this button to the panel
    • instantiates an object of the JTextField class called colorIt
      • its constructor determines the width is 20
    • adds this label 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
  • an if ( expression ) else if ( expression ) code segment is used to determine which button the user clicked
    • the expressions are based on userClick.getSource( )
    • if they clicked cmdRed
      • the background color of the JTextField named colorIt is changed to red
    • if they clicked cmdGreen
      • the background color of the JTextField named colorIt is changed to green
    • if they clicked cmdBlue
      • the background color of the JTextField named colorIt is changed to blue

The following contains the MultipleChoice.html file. The size affects the appearance significantly.

 

<html>
<applet code="MultipleChoice.class" width=250 height=100>
</applet>
</html>

 

When this applet is run and you press the button that says Red, you should see the following on your screen.

 

 

You can keep clicking on buttons until you get tired of it.