1

I have a button which I want to do something different the second time it is clicked. For this purpose I have the clicked button handler check for the button's name, and I change this name the first time it is pressed.

The button name does not change though, do I need to repack or update the buttonpanel it belongs to somehow?

@Override
    public void actionPerformed(ActionEvent e) {
        switch(((JButton) e.getSource()).getName()) {
        case "TR":
                ((JButton) e.getSource()).setName("Done TR");
                awaitPoints("TR");
                break;
        case "Done TR":
                CP.ComputeTransfo();
                ((JButton) e.getSource()).setName("TR Complete");
                ((JButton) e.getSource()).setEnabled(false);
                break;
        default:
                System.out.println("ERROR:TFM01");
                System.exit(0);
                break;
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Zyzyx
  • 492
  • 4
  • 18

1 Answers1

3

Regarding to this answer

Component.setName(..) is used in the JDK mostly by the look and feel implementation classes to set ID-like strings for each component, e.g. BasicOptionPaneUI might call it on a button component to set its name to "OptionPane.button".

so if you want to change the text of the label you need to use setText() not setName()

JButton btn=new JButton("Text Before the change");
btn.setText("This text got changed!")
Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34