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() );

        add(field);
        add(button);



    }

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

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(400,400);
        f.setLocationRelativeTo(null);
        f.setVisible(true);




    }


    class ButtonActionListener implements ActionListener
    {

        ButtonActionListener()
        {

        }
        @Override        public void actionPerformed(ActionEvent e) {

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


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



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


}
]

Result:
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.

JButton
ActionListener

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators 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 php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
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
https://4.bp.blogspot.com/-6LRCYdINSq4/WJyNAvmIouI/AAAAAAAAG2M/bR1ZaXBpQok0T0n9LY3yos9pIAiLmp2xgCEw/s200/Jbutton-actionListener-code-example.PNG
https://4.bp.blogspot.com/-6LRCYdINSq4/WJyNAvmIouI/AAAAAAAAG2M/bR1ZaXBpQok0T0n9LY3yos9pIAiLmp2xgCEw/s72-c/Jbutton-actionListener-code-example.PNG
Java Codes
http://www.download-all.net/2017/02/how-to-implement-jbuttonaddactionlisten.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/02/how-to-implement-jbuttonaddactionlisten.html
true
7826648292785071009
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