User Interface Actions

helloButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
textField.setText("Hello, World");
}
});

Accessing Variables from Enclosing Scope

User Interface Actions

See: ActionTest.java

Constructing Related Actions

public static ActionListener createGreetingButtonListener(
final String message)
{
return new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
textField.setText(message);
}
};
}

See: ActionTest2.java


ActionTest.java


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

public class ActionTest
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();

      final int FIELD_WIDTH = 20;
      final JTextField textField = new JTextField(FIELD_WIDTH);
      textField.setText("Click a button!");

      JButton helloButton = new JButton("Say Hello");

      helloButton.addActionListener(new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               textField.setText("Hello, World!");
            }
         });


      JButton goodbyeButton = new JButton("Say Goodbye");

      goodbyeButton.addActionListener(new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               textField.setText("Goodbye, World!");
            }
         });

      Container contentPane = frame.getContentPane();
      contentPane.setLayout(new FlowLayout());

      contentPane.add(helloButton);
      contentPane.add(goodbyeButton);
      contentPane.add(textField);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setVisible(true);
   }
}


ActionTest2.java


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

public class ActionTest2
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();

      final int FIELD_WIDTH = 20;
      textField = new JTextField(FIELD_WIDTH);
      textField.setText("Click a button!");

      JButton helloButton = new JButton("Say Hello");

      helloButton.addActionListener(
         createGreetingButtonListener("Hello, World!"));

      JButton goodbyeButton = new JButton("Say Goodbye");

      goodbyeButton.addActionListener(
         createGreetingButtonListener("Goodbye, World!"));

      Container contentPane = frame.getContentPane();
      contentPane.setLayout(new FlowLayout());

      contentPane.add(helloButton);
      contentPane.add(goodbyeButton);
      contentPane.add(textField);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setVisible(true);
   }

   public static ActionListener createGreetingButtonListener(
      final String message)
   {
      return new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               textField.setText(message);
            }
         };
   }

   private static JTextField textField;
}

Results

After pressing hello:

After pressing goodbye:


Maintained by John Loomis, updated Sat Feb 24 11:35:04 2007