How to add ActionListener to JButton

How to add ActionListener to JButton

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


COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: How to add ActionListener to JButton
How to add ActionListener to JButton
How to add ActionListener to JButton
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZkhF8QoeP4kWbdOgISrcSdDffhYgcoaQCJKv52qKsj14lV0sd47avppVtt4YxHgj3mJNk7wN81F9mQdV_I8Zgh8hDl5dagLnvVs-XXY-sYRnpRsUV1maUt3BpYfngQMG__XXselSI2XhG/s320/how-to-add-ActionListener-JButton-min.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZkhF8QoeP4kWbdOgISrcSdDffhYgcoaQCJKv52qKsj14lV0sd47avppVtt4YxHgj3mJNk7wN81F9mQdV_I8Zgh8hDl5dagLnvVs-XXY-sYRnpRsUV1maUt3BpYfngQMG__XXselSI2XhG/s72-c/how-to-add-ActionListener-JButton-min.PNG
Code WHIZZ
https://code-whizz.blogspot.com/2017/03/how-to-implement-actionlistener.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2017/03/how-to-implement-actionlistener.html
true
5534598864497432585
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy