How to Implement JButton.addActionListener() Method in Java Example

How to Implement JButton.addActionListener() Method in Java Example

This is a common question, How do I addActionListener to my JButton so that when the user clicks it, action is invoked.It's pretty simple.My last post was about setting Windows LookAndFeel using UIManager .I added JButton, JTextFields, and JTextArea but no action listener was added.

Adding JButton actionListener.

Run below code.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.JOptionPane;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JButtonAddActionListener extends JPanel {
    public JTextField  field;
    public JButton button;
    public JButtonAddActionListener()
        field=new JTextField("Enter name");
        button=new JButton("Click me");
        button.addActionListener(new ButtonActionListener() );



    public static void main(String args[])
        JFrame f=new JFrame("Simple Action Listener program example");
        f.getContentPane().add(new JButtonAddActionListener () );



    class ButtonActionListener implements ActionListener


        @Override        public void actionPerformed(ActionEvent e) {

            String text=field.getText();
            JOptionPane.showMessageDialog(null,"Message entered is \n" +text,
                    "Action Listener Example",

            throw new UnsupportedOperationException("Not supported yet.");

//To change body of generated methods, choose Tools | Templates.


How to implement JButton Action Listener

JButton.addActionListener() Code Explanation

In this article  have introduced more swing components i.e

Others are JFrame methods.

  •  f.getContentPane().add(new JButtonAddActionListener () )-adds the JPanel to JFrame
  •  new JButtonAddActionListener ()-creates a new JButtonAddActionListener() class.
  •     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)-closes the JFrame window          when  X button is clicked and exits the program
  •      f.setSize(400,400)-sets the window size.
  •      f.setLocationRelativeTo(null)-positions the JFrame window at the center of the screen
  •      f.setVisible(true)-displays the JFrame window.
 class ButtonActionListener is a inner class that has access to public variables of the outer class and that the reason why we can be able to getText() entered in JTextField field

To read more about JButton and ActionListener classes check below Javadoc documents.




android android Apps android emulators antivirus arrays blogger bluestack Break Statement in Java constructors download downloader drivers emulators fragment java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql node.js php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
Java Codes: How to Implement JButton.addActionListener() Method in Java Example
How to Implement JButton.addActionListener() Method in Java Example
How to Implement JButton.addActionListener() Method in Java Example
Java Codes
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