banner



How To Use Action Listener To Update Jframe

Add ActionListener to JButton in Java

  1. Add together ActionListener to a JButton Using an Bearding Class
  2. Add ActionListener to JButton Using Anonymous Class and Lambda
  3. Add ActionListener to JButton Using Inheritance

Today we will talk over the ActionListener interface of the java.awt.outcome and how to add it to a JButton, a component of the JFrame class in the Java Swing package.

Add together ActionListener to a JButton Using an Anonymous Class

In the first example, we create a class JavaExample with a method chief(), and in that method, we create a JFrame object. In the JFrame window, we create three components: a JLabel to prove a bulletin, a JTextField to take an input, and a JButton to which we will add the ActionListener.

To add the listener, nosotros call the addActionListener() function of the JButton class, and in this method, we create an bearding course and override the actionPerformed() function that is a part of the ActionListener interface. actionPerformed() is a method that is chosen when an action is performed.

In the actionPerformed() method, we first check if there is anything in the JTextField, and if the Text Field is empty, then we bear witness a message using JLabel. Otherwise, we display the message that we write in the Text Field.

              import javax.swing.*; import coffee.awt.event.ActionEvent; import java.awt.event.ActionListener;  public class JavaExample {     public static void main(String[] args) {          JFrame jFrame = new JFrame("Java Example");          JLabel jLabel = new JLabel();         jLabel.setBounds(50, 150, 350, 40);          concluding JTextField jTextField = new JTextField();         jTextField.setBounds(50, 50, 150, 20);          JButton jButton = new JButton("Submit");         jButton.setBounds(50, 100, 100, 30);          jButton.addActionListener(new ActionListener() {             @Override             public void actionPerformed(ActionEvent e) {                 if (!jTextField.getText().equals(""))                     jLabel.setText(jTextField.getText());                 else                     jLabel.setText("Please write something in the edit box");             }         });         jFrame.add together(jLabel);         jFrame.add(jButton);         jFrame.add together(jTextField);         jFrame.setSize(400, 400);         jFrame.setLayout(nil);         jFrame.setVisible(true);     } }                          

Output:

add actionlistener to jbutton 1

Add ActionListener to JButton Using Anonymous Class and Lambda

In Java 8, Lambda Expressions were added that nosotros would use in this case. Everything is the aforementioned every bit the previous example, only in the program, we create a method buttonPressed(), and in that role, we print the message written in the text field to a JLabel.

To add together the ActionListener to the JButton, nosotros use the addActionListener() function, and in that method, nosotros employ the lambda approach. Nosotros use the parameter e that is an ActionEvent object, then call the buttonPressed() method.

              import javax.swing.*;  public grade JavaExample {     static JTextField jTextField;     static JLabel jLabel;      public static void chief(String[] args) {          JFrame jFrame = new JFrame("Java Instance");          jLabel = new JLabel();         jLabel.setBounds(50, 150, 350, 40);          jTextField = new JTextField();         jTextField.setBounds(50, 50, 150, 20);          JButton jButton = new JButton("Submit");         jButton.setBounds(l, 100, 100, 30);          jButton.addActionListener(e -> buttonPressed());                  jFrame.add together(jLabel);         jFrame.add(jButton);         jFrame.add together(jTextField);                  jFrame.setSize(400, 400);         jFrame.setLayout(null);         jFrame.setVisible(true);     }      static void buttonPressed() {         if (!jTextField.getText().equals("")) {             jLabel.setText(jTextField.getText());         } else {             jLabel.setText("Please write something in the edit box");         }     } }                          

Output:

add actionlistener to jbutton 2

Add ActionListener to JButton Using Inheritance

In this program, nosotros add together the ActionListener using the concept of inheritance.

Nosotros create a class JavaExample and so extend the grade JFrame and implement the ActionListener interface. We accept to override the actionPerformed() method that is a part of the ActionListener interface.

Nosotros declare the JFrame component variables outside the main() method because we demand to use it outside the part. Nosotros create a constructor of the JavaExample class and set all the components in it.

To add the ActionListener to the JButton component, nosotros call the addActionListener() method and pass this, which points to the context of the electric current grade equally the current grade JavaExample implements ActionListener it is a valid context to pass in the function.

In the actionPerformed() function, we write the operations that we want to perform when the button is pressed. In the main() method, nosotros create an instance of JFrame and prepare its layout and visibility.

              import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;  public class JavaExample extends JFrame implements ActionListener {     static JTextField jTextField;     static JLabel jLabel;     static JButton jButton;      public static void main(String[] args) {         JFrame jFrame = new JavaExample();         jFrame.setLayout(null);         jFrame.setVisible(true);     }      public JavaExample() {         jLabel = new JLabel();         jLabel.setBounds(fifty, 150, 350, 40);          jTextField = new JTextField();         jTextField.setBounds(fifty, 50, 150, 20);          jButton = new JButton("Submit");         jButton.setBounds(fifty, 100, 100, 30);          jButton.addActionListener(this);          add(jLabel);         add(jButton);         add(jTextField);          setSize(400, 400);      }      @Override     public void actionPerformed(ActionEvent east) {         if (!jTextField.getText().equals("")) {             jLabel.setText(jTextField.getText());         } else {             jLabel.setText("Delight write something in the edit box");         }     } }                          

Output:

add actionlistener to jbutton 3

Write for united states

DelftStack manufactures are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid manufactures, you can cheque the write for united states of america page.

Related Article - Java GUI

  • Create Coffee Progress Bar Using JProgressBar Class
  • Tic Tac Toe GUI in Java
  • The pack() Method in Java
  • Ezoic

    How To Use Action Listener To Update Jframe,

    Source: https://www.delftstack.com/howto/java/add-actionlistener-to-jbutton-in-java/

    Posted by: rogersbethen.blogspot.com

    0 Response to "How To Use Action Listener To Update Jframe"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel