Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When testing JButton my lambda expression works, but actionPerformed does not

I’m learning how to work with Java Swing for the first time by following a tutorial on YouTube. I have reached the section covering buttons and have been following the code to the T. However, while attempting to test the button so it would print out the word "test" when I’m using the actionPerformed method, my button would not print out the word.

You can find the original code for that test here:

Main.java

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

package com.learnjava;
public class Main {

    public static void main(String[] args) {

        new MyFrame();

    }
}

MyFrame.java

package com.learnjava;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyFrame extends JFrame implements ActionListener{
    
    JButton button;

    MyFrame() {

        JButton button = new JButton();
        button.setBounds(200, 100, 100, 50);
        button.addActionListener(this);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500, 500);
        this.setLayout(null);
        this.setVisible(true);
        this.add(button);


    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == button) {
            System.out.println("test");
        }
        
    }
}

Whenever I pressed the button using the previous code, it wouldn’t print out the word "test". If I used a lambda expression, it would work.

MyFrame.java (updated with lambda expression)

package com.learnjava;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyFrame extends JFrame implements ActionListener{
    
    JButton button;

    MyFrame() {

        JButton button = new JButton();
        button.setBounds(200, 100, 100, 50);
        button.addActionListener(e -> {System.out.println("test");}); // updated with lambda expression

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500, 500);
        this.setLayout(null);
        this.setVisible(true);
        this.add(button);


    }
}

I’ve been trying to find a reason online as to why the former code does not work. I’ve seen one question but it was left unanswered. While I could settle for using the lambda expression instead, I would still like to understand how to write the former code correctly if I was doing something wrong. Thank you in advanced!

Note: If it is relevant for some reason, the IDE I am using is IntelliJ and my JDK is version 12.

>Solution :

Change:

JButton button = new JButton(); 

To:

button = new JButton();

The first is a local variable that shadows the class attribute.

The 2nd class has the same problem, but since it (button creation, adding the listener, adding it to the GUI) is all done in the one code section, it is not obvious that the class attribute is never used.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading