Home > front end >  When testing JButton my lambda expression works, but actionPerformed does not
When testing JButton my lambda expression works, but actionPerformed does not

Time:01-08

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

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.

CodePudding user response:

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.

  •  Tags:  
  • Related