P
P
Pavel2020-07-06 10:33:47
Java
Pavel, 2020-07-06 10:33:47

Error while trying to get text of JButton (java)?

There is a JFrame, it has a large number of JButtons. Each Jbutton has an ActionListener attached to it.
Button example:

static JButton one = new JButton("1");

public static void main(String[] args) {
   one.addActionListener(new equation());
}


Handler class:
class equation implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    String name = e.getActionCommand().getText();  // =ошибка
  }
}


The error "The method getText() is undefined for the type String" occurs.
Tried to use getName() instead of getText() - same error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-07-06
@indexOff

That's right, the ActionEvent.getActionCommand() method returns a string. The string does not have a getText method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question