Action Listener Simple

My Stuff


Compact version using lambda expressions



// File: BasicGUIv001.java
// Author: Nicholas Duchon
// Date: Oct 29, 2009
// History:
//    Sep 19, 2015 - modified to use lambda expressions and overloaded function addRow

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;

public class BasicGUIv001
   extends JFrame
{

   public static final long serialVersionUID = 123; // ND: not general!
 
   JTextField jtz = new JTextField ();
 
   public void addRow (String s) {
      JButton    jb = new JButton (s);
      JTextField jt = new JTextField (s);
      add (jb);
      add (jt);
      jb.addActionListener (e -> jtz.setText ("Button " + s));
      jt.addActionListener (e -> jtz.setText ("TextField: " + jt.getText()));
   } // String addRow - middle

   public BasicGUIv001 (String title) {
      super (title);
      setLayout (new java.awt.GridLayout (0, 2));
  
      add (new JLabel ("Press buttons", JLabel.CENTER));
      add (new JLabel (" Hit return for text fields ", JLabel.CENTER));

      String [] pans = {"one", "two", "three", "four", "five", "six"};
      for (String s: pans) addRow (s);

      add (new JLabel ("Input echo:", JLabel.RIGHT));
      add (jtz);
  
      pack (); // good if all elements known now
      setLocationRelativeTo (null);
      setVisible (true); // show the frame
      setDefaultCloseOperation (EXIT_ON_CLOSE);
   } // end String constructor
 
   public static void main (String args []) {
      new BasicGUIv001 ("BasicGUIv001"); // create a display window
   } // end call with main, also.
 
} // end class BasicGUI

Original version:

// File: BasicGUI.java
// Author: Nicholas Duchon
// Date: Oct 29, 2009

// To run as a web page, see the javadoc comments

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

public class BasicGUI
extends JFrame
implements ActionListener
{

JLabel jhb = new JLabel ("Press buttons", JLabel.CENTER);
JLabel jht = new JLabel ("Hit return for text fields", JLabel.CENTER);

JButton jb1 = new JButton ("one");
JButton jb2 = new JButton ("two");
JButton jb3 = new JButton ("three");
JButton jb4 = new JButton ("four");
JButton jb5 = new JButton ("five");
JButton jb6 = new JButton ("six");

JTextField jt1 = new JTextField ("one");
JTextField jt2 = new JTextField ("one");
JTextField jt3 = new JTextField ("one");
JTextField jt4 = new JTextField ("one");
JTextField jt5 = new JTextField ("one");
JTextField jt6 = new JTextField ("one");

JLabel jL1 = new JLabel ("Input echo:", JLabel.RIGHT);

JTextField jtz = new JTextField ();

public BasicGUI () {
Container c = getContentPane ();
c.setLayout (new GridLayout (0, 2));

c.add (jhb);
c.add (jht);

c.add (jb1);
c.add (jt1);
c.add (jb2);
c.add (jt2);
c.add (jb3);
c.add (jt3);
c.add (jb4);
c.add (jt4);
c.add (jb5);
c.add (jt5);
c.add (jb6);
c.add (jt6);
c.add (jL1);
c.add (jtz);

jb1.addActionListener (this);
jb2.addActionListener (this);
jb3.addActionListener (this);
jb4.addActionListener (this);
jb5.addActionListener (this);
jb6.addActionListener (this);

jt1.addActionListener (this);
jt2.addActionListener (this);
jt3.addActionListener (this);
jt4.addActionListener (this);
jt5.addActionListener (this);
jt6.addActionListener (this);

} // end method init

public void actionPerformed (ActionEvent e) {
Object s = e.getSource ();
if (s == jb1) jtz.setText ("Button one");
if (s == jb2) jtz.setText ("Button two");
if (s == jb3) jtz.setText ("Button three");
if (s == jb4) jtz.setText ("Button four");
if (s == jb5) jtz.setText ("Button five");
if (s == jb6) jtz.setText ("Button six");

if (s == jt1) jtz.setText (jt1.getText());
if (s == jt2) jtz.setText (jt2.getText());
if (s == jt3) jtz.setText (jt3.getText());
if (s == jt4) jtz.setText (jt4.getText());
if (s == jt5) jtz.setText (jt5.getText());
if (s == jt6) jtz.setText (jt6.getText());
} // end method ActionPerformed

public static void main (String args []) {
String title = "BasicGUI"; // title of window
JFrame hostFrame = new BasicGUI (); // create a display window
hostFrame.setTitle (title); // set window's title
hostFrame.setSize (300,200); // set the display window size, MUST set this value
hostFrame.setVisible (true); // show the frame
hostFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
} // end call with main, also.

} // end class BasicGUI