Search code examples
javaswingjbuttonactionlistener

How to determine the event source in an ActionListener?


Ok. I'm not sure about the title of my question and whether I used the right words. As I am a self taught total amateur I'm finding it hard to ask my question as I don't know the correct terms for things so I will write something in code and then ask my question. I've written it without import statements, setting up layouts and scrollbars and some other things just to keep it simpler.

public class Foo{
    JTextArea text;

    public static void main(String[] args){
        Foo foo = new Foo;
        foo.go();
    }

    public void go(){
        JFrame frame = new JFrame();
        JButton button = new JButton("One");
        JButton button2 = new JButton("Two");
        JPanel panel = new JPanel();

        frame.setVisible(true);
        frame.setSize(600, 300);

        frame.getContentPane().add(BorderLayout.EAST, panel);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(button);
        panel.add(button2);

        text = new JTextArea(10, 20);
        panel.add(text);

        button.addActionListener(new ButtLis());
        button2.addActionListener(new ButtLis());
    }

    class ButtLis implements ActionListener{
        @override
         // this is where I have the problem

        text.append();
    }

}

What I want is an if statement to go into my inner class (ButtLis) which will determine which of the buttons are pressed and then append certain text to the JTextArea based on that. But I don't know what to call to find out which button was pressed.


Solution

  • You have a couple options. In the current case you have, where the JButton objects are locally scoped within the constructor, you would need to check for actionCommmand because the objects are not accessible from the ActionListener with their current scope. So you could do this

    class ButtLis implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if ("One".equals(command)) {
                // do something
            }
        }
    }
    

    If you wanted to compare object source, you would need to give your buttons a global scope

    public class Foo {
        JButton button = new JButton("One");
        JButton button2 = new JButton("Two");
    
        class ButtLis implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == button) {
    
                }
            }
        }
    }
    

    A third option is to register the buttons individually

    public void go() {
        ...
        button.addActionListener(new ActionListener(){
             @Override
             public void actionPerformed(ActionEvent e) {
                 // do something
             }
        });
    }
    

    See more at How to use Common Button and How to Write ActionListeners