Question

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.

Was it helpful?

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

OTHER TIPS

I think this is what you're looking for, although I would hardly recommend it:

class ButtLis implements ActionListener {
    private JTextArea text;

    public ButtLis(JTextArea text) {
        this.text = text;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton)e.getSource(); // Warning! This is not good coding practice, because you don't know that the source will be a button
        text.append(button.getText());
    }
}

Instead, I'd recommend:

JButton button1 = new JButton("One");
button1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        text.append("one");
    }
});

That uses an "anonymous inner class" to define the action listener. For Button2, you'd say a similar thing. The benefits here are that the action listener is right next to the button that it works on, and it prevents you from having a single ActionListener that has to check where each event came from (using e.getSource()).

Inside your ButtLis, add this

class ButtLis implements ActionListener {
    public void actionPerformed(ActionEvent e) {
       e.getSource();
       //Your implementation
    }
}

Here the class that implements ActionListener:

class ButtLis implements ActionListener {

    JTextArea text;

    public ButtLis(JTextArea text) {
        this.text = text;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() instanceof JButton) {
            JButton button = (JButton) ae.getSource();
            if(text != null){
                text.append(" " + button.getText());
            }
        }
    }
}

And here how to add an action listener to the buttons:

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

For a generale ActionListsner, i suggest a different customer ActionListener like this:

abstract class ButtLis implements ActionListener {

    protected String sourceEvent;  //or you can use a reference for the source object

    public ButtLis(String sourceEvent) {
        this.sourceEvent = sourceEvent;
    }

    public String getSourceEvent() {
        return sourceEvent;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        customer_actionPerformed(ae);
    }

    public abstract void customer_actionPerformed(ActionEvent ae);
}

And the adding of action listener for any component is the same as an ordinary ActionListener:

//for exemple button
button.addActionListener(new ButtLis(button.getText()) {
    @Override
    public void customer_actionPerformed(ActionEvent ae) {        
        text.append(getSourceEvent());
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top