ActionListener And Action Class Level Variable Swing Event

Chapter: Action Listener Last Updated: 08-08-2016 18:32:20 UTC

Program:

            /* ............... START ............... */
                
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class JavaClasslevelActionListner extends JFrame {

	JTextField field = new JTextField();

	public static void main(String[] args) {
		JavaClasslevelActionListner listner = new JavaClasslevelActionListner();
	}

	public JavaClasslevelActionListner() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JButton button = new JButton("Press me");
		button.addActionListener(new ButtonListener());
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(button, BorderLayout.SOUTH);
		this.getContentPane().add(field, BorderLayout.NORTH);
		this.pack();
		this.setVisible(true);
	}

	class ButtonListener implements ActionListener {
		boolean toggle = true;

		public void actionPerformed(ActionEvent e) {
			if (toggle)
				field.setText("JavaScan");
			else
				field.setText("Hello");
			toggle = !toggle;
		}
	}
}
                /* ............... END ............... */
        

Output


ActionListener And Action Class Level Variable Swing Event

Tags

ActionListener And Action Class Level Variable, Java, ActionListener,Swing

Similar Programs Chapter Last Updated
Action Listener To JComboBox In Java Action Listener 22-09-2018
Add Action Listener To JTextField In Java Action Listener 08-08-2016
Get Event Source From ActionEvent Swing Java Action Listener 08-08-2016
ActionListener Inner Class To Handle Event In Java Action Listener 28-07-2016
ActionListener Event Source In Java ActionPerformed Method Action Listener 28-07-2016
ActionListener Handling An Action In Java Action Listener 28-07-2016

1