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.
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);
...
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With