ActionListener Handling An Action In Java

Chapter: Action Listener Last Updated: 28-07-2016 18:16:40 UTC

Program:

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

class MyActionListener implements ActionListener {
	public void actionPerformed(ActionEvent e) {
		JButton source = (JButton) e.getSource();
		String buttonText = source.getText();
		JOptionPane.showMessageDialog(null, "You clicked" + buttonText);
	}
}

public class JavaActionListenerHandling {

	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		JFrame frame = new JFrame("ActionListener Test 1");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JButton button = new JButton("Register");
		button.addActionListener(new MyActionListener());
		frame.getContentPane().add(button);
		frame.pack();
		frame.setVisible(true);
	}
}
                /* ............... END ............... */
        

Output


ActionListener Handling An Action In Java

Tags

ActionListener Handling An Action, 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 Inner Class To Handle Event In Java Action Listener 28-07-2016
ActionListener Event Source In Java ActionPerformed Method Action Listener 28-07-2016

1