0

How do I tell my action listener to click a button and display the buttons text into a text area?

ActionListener listener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if (e.getSource() instanceof JButton) {
                String text = e.getActionCommand();
                JOptionPane.showMessageDialog(null, text);
            }
        }
    };

The JTextArea area is initialized after the actionlistener and so when i try to define area. it give me an error. Can you help me?

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    What error do you get? – katwekibs Jun 11 '20 at 18:38
  • 1
    It should work are you sure you are adding the listener listener to the button instance? an error you are getting will be of use to see what problem you are having – katwekibs Jun 11 '20 at 18:43
  • 1
    @katwekibs OP is asking how to click a button from inside their action listener. – SnakeDoc Jun 11 '20 at 18:43
  • 1
    You need to call method `addActionListener()` of class `JButton`. The `actionPerformed()` method doesn't click the button. Maybe you could [edit] your question and post a [mcve]? – Abra Jun 11 '20 at 18:44
  • 1
    I recommend [How to Write an Action Listener](https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) – Abra Jun 11 '20 at 18:49
  • 1
    OP has a perfectly well written action listener... they're clearly asking _how_ to click a different button from within the action listener. Likely to invoke some behavior in addition to the button-that-was-clicked's default behavior. The easiest way to do that is to call `doClick()` on the other button. – SnakeDoc Jun 11 '20 at 18:52
  • 1
    Any time “it give me an error” you need to [tell us what the error is](https://idownvotedbecau.se/noexceptiondetails/). – Dour High Arch Jun 11 '20 at 20:09
  • 1
    See the [Calculator Panel](https://stackoverflow.com/questions/33739623/how-to-add-a-shortcut-key-for-a-jbutton-in-java/33739732#33739732) example. It shows how to create a shared Action that will add the text of the button to a Swing component. – camickr Jun 12 '20 at 00:31

1 Answers1

2

How do I tell my action listener to click a button

To "click" the button, you simply call that button's doClick() method.

From the JavaDoc:

public void doClick()

Programmatically perform a "click". This does the same thing as if the user had pressed and released the button.

If your buttons are global, you can simply invoke the method like:

someButton.doClick();

If they aren't global, you will need to somehow obtain a reference to the button:

someContainer.getSomeButton().doClick();

The second part of your question asks:

The JTextArea area is initialized after the actionlistener and so when i try to define area. it give me an error.

You can try checking if the JTextArea has been initialized already, and if not, initialize it at that moment. Something like:

if (area == null) {
    area = new JTextArea();
    // do the rest of your initialization and placement in the UI
}
// continue with writing text into the `area` and your other program logic
area.append(myText);
// etc...
SnakeDoc
  • 13,611
  • 17
  • 65
  • 97