Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to Reset Buttons?

In my program, I have 12 different toggle buttons that will need to be reset at the same time. Instead of writing

buttonOne.setText("");
buttonOne.setSelected(false);
buttonOne.setEnabled(true);

over and over for 12 different toggle buttons, is there a way to do this in a method by passing parameters? I've only recently started java and i've never used parameter declarations that aren't strings or ints, so I was not sure if there would be a way to do it with a toggle button.

like image 680
quinny Avatar asked Dec 24 '22 17:12

quinny


2 Answers

You could pass the button in as a parameter to a new method, and call your methods on that parameter

private void toggleButton(JToggleButton button) {
    button.setText("");
    button.setSelected(false);
    button.setEnabled(true);
}

// ...

toggleButton(buttonOne);
toggleButton(buttonTwo);
...
like image 131
drvdijk Avatar answered Dec 28 '22 08:12

drvdijk


If You want to trigger all those buttons at once then you can put those buttons in a list and do:

for (JButton button : myListOfButtons) {
     button.setText("");
     button.setSelected(false);
     button.setEnabled(true);
}
like image 44
ΦXocę 웃 Пepeúpa ツ Avatar answered Dec 28 '22 10:12

ΦXocę 웃 Пepeúpa ツ