ActionListener Inner Class To Handle Event In Java

Chapter: Action Listener Last Updated: 28-07-2016 18:42:00 UTC

Program:

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

public class JavaInnerClassActionListner extends JFrame {

	private JButton button1 = new JButton("Click Me!");

	public static void main(String[] args) {
		new JavaInnerClassActionListner();
	}

	public JavaInnerClassActionListner() {
		this.setSize(200, 100);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("I'm Listening");

		ClickListener cl = new ClickListener();
		JPanel panel1 = new JPanel();

		button1.addActionListener(cl);
		panel1.add(button1);
		this.add(panel1);

		this.setVisible(true);
	}

	private class ClickListener implements ActionListener {
		private int clickCount = 0;

		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == button1) {
				clickCount++;
				if (clickCount == 1)
					button1.setText("I've been clicked ");
				else
					button1.setText("I've been clicked!" + clickCount + " times!");
			}
		}
	}
}
                /* ............... END ............... */
        

Output


ActionListener Inner Class To Handle Event In Java

Tags

ActionListener Event Source, Java, ActionListener

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 And Action Class Level Variable Swing Event Action Listener 08-08-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