1

When I compile it show error in line 33 : Cannot find symbol. I am calling jbtNew.addActionListener(listener), so why it's unable to find jbtNew in

(e.getSource() == jbtNew) in line 33.

from code

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AnonymousListenerDemo extends JFrame {

    public AnonymousListenerDemo() {
        // Create four buttons
        JButton jbtNew = new JButton("New");
        JButton jbtOpen = new JButton("Open");
        JButton jbtSave = new JButton("Save");
        JButton jbtPrint = new JButton("Print");
        // Create a panel to hold buttons
        JPanel panel = new JPanel();
        panel.add(jbtNew);
        panel.add(jbtOpen);
        panel.add(jbtSave);
        panel.add(jbtPrint);

        add(panel);
        // Create and register anonymous inner-class listener
        AnonymousListenerDemo.ButtonListener listener = new AnonymousListenerDemo.ButtonListener();
        jbtNew.addActionListener(listener);


    }

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == jbtNew) //Here it show the problem
            {
                System.out.println("Process New");
            }
        }
    }

    /** Main method */
    public static void main(String[] args) {
        JFrame frame = new AnonymousListenerDemo();
        frame.setTitle("AnonymousListenerDemo");
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

2 Answers 2

3

That's a local variable.
It doesn't exist outside the constructor.

You need to make a field in the class.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't notice that at first; local is worse than private data member. As written, no one will have the use of it once you exit the ctor.
1

this could be work (in the form as you posted here) and @SLaks mentioned +1, with a few major changes

in the case that all methods will be placed into separated classes to use put/getClientProperty()

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AnonymousListenerDemo {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame("AnonymousListenerDemo");
    // Create four buttons
    private JButton jbtNew = new JButton("New");
    private JButton jbtOpen = new JButton("Open");
    private JButton jbtSave = new JButton("Save");
    private JButton jbtPrint = new JButton("Print");

    public AnonymousListenerDemo() {
        JPanel panel = new JPanel();// Create a panel to hold buttons
        panel.add(jbtNew);
        panel.add(jbtOpen);
        panel.add(jbtSave);
        panel.add(jbtPrint);
        // Create and register anonymous inner-class listener
        jbtNew.addActionListener(new ButtonListener());
        frame.add(panel);
        //frame.setTitle("AnonymousListenerDemo");
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == jbtNew) {
                System.out.println("Process New");
            }
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AnonymousListenerDemo();
            }
        });
    }
}

2 Comments

can u explain what is EventQueue.invokeLater(new Runnable()
@NishitShuvo: See also Initial Threads.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.