How to add ActionListener to JButton
There are different ways which one can add ActionListener to JButton .If you implement ActionListener interface you are required to override public void actionPerformed() abstract method.You should check out How to implement WindowListener interface.
How to add ActionListener to JButton using Anonymous class
Anonymous inner class means a class with no name.
[
import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; public class JFrameMethods extends JFrame{ public JTextField username,password; JButton login,cancel; JFrameMethods() { super("How to add ActionListener to JButton "); username=new JTextField("Enter Username"); password=new JTextField("Enter Password"); login=new JButton("Login"); login.setToolTipText("Click here to Login"); cancel=new JButton("Cancel"); cancel.setToolTipText("Click here to close this application"); username.setBounds(10, 10, 200, 20); password.setBounds(10, 40, 200, 20); login.setBounds(20,80, 100, 30); cancel.setBounds(140,80, 100, 30); add(username); add(password); add(login); add(cancel); login.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { String user=username.getText(); String pass=password.getText(); JOptionPane.showMessageDialog(null, "Your Username is "+user+ "/n "
+ "Password is "+pass, "ActionListener Example", JOptionPane.INFORMATION_MESSAGE); } }); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { System.exit(0); } }); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setLocationRelativeTo(null); setLayout(null); setSize(400,400); setVisible(true); } public static void main(String args[]) { new JFrameMethods(); } }
]
How to add ActionListener to JButton using Inner class
[
import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; public class JFrameMethods extends JFrame{ public JTextField username,password; JButton login,cancel; JFrameMethods() { super("How to add ActionListener to JButton "); username=new JTextField("Enter Username"); password=new JTextField("Enter Password"); login=new JButton("Login"); login.setToolTipText("Click here to Login"); cancel=new JButton("Cancel"); cancel.setToolTipText("Click here to close this application"); username.setBounds(10, 10, 200, 20); password.setBounds(10, 40, 200, 20); login.setBounds(20,80, 100, 30); cancel.setBounds(140,80, 100, 30); add(username); add(password); add(login); add(cancel); login.addActionListener(new ButtonListener()); cancel.addActionListener(new ButtonListener()); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setLocationRelativeTo(null); setLayout(null); setSize(400,400); setVisible(true); } public static void main(String args[]) { new JFrameMethods(); } //Inner class that implements ActionListener
class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { if(evt.getSource()==login) { String user=username.getText(); String pass=password.getText(); JOptionPane.showMessageDialog(null, "Your Username is "+user+ "/n " + "Password is "+pass,"ActionListener Example", JOptionPane.INFORMATION_MESSAGE); } else if(evt.getSource()==cancel) { System.exit(0); } } } }
]
How to add ActionListener to JButton by implementing the interface
[
import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; public class JFrameMethods extends JFrame implements ActionListener{ public JTextField username,password; JButton login,cancel; JFrameMethods() { super("How to add ActionListener to JButton "); username=new JTextField("Enter Username"); password=new JTextField("Enter Password"); login=new JButton("Login"); login.setToolTipText("Click here to Login"); cancel=new JButton("Cancel"); cancel.setToolTipText("Click here to close this application"); username.setBounds(10, 10, 200, 20); password.setBounds(10, 40, 200, 20); login.setBounds(20,80, 100, 30); cancel.setBounds(140,80, 100, 30); add(username); add(password); add(login); add(cancel); //adding actionlistener to Login button
login.addActionListener(this); //adding actionlistener to cancel button
cancel.addActionListener(this); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setLocationRelativeTo(null); setLayout(null); setSize(400,400); setVisible(true); } //Overriding interface abstract method public void actionPerformed
public void actionPerformed(ActionEvent evt) { if(evt.getSource()==login) { String user=username.getText(); String pass=password.getText(); JOptionPane.showMessageDialog(null, "Your Username is "+user+ "/n " + "Password is "+pass,"ActionListener Example", JOptionPane.INFORMATION_MESSAGE); } else if(evt.getSource()==cancel) { System.exit(0); } } //main method public static void main(String args[]) { new JFrameMethods(); } }
]
Code Explanation:
Above code has two classes.One class is the outer class JFrameMethods and the inner class is ButtonListener which implements Interface actionListener.
When the login button is clicked,String texts in the two JTextFields is retrieved using JTextField.getText() method. A JOptionPane.showMessageDialog pops up showing the two strings entered.
Other methods used in this application
- [setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);]-exits application when close button is clicked.
- [setSize(400,400);]-sets jframe window size.
- [setLocationRelativeTo(null);]-positions Jframe at the center of the window.