If you want to reset all buttons in a panel or frame, you can use this method:
private void clearAllFields() {
for (Component C : myPanel.getComponents()) {
if (C instanceof JButton) {
((JButton) C).setText("");
((JButton) C).setEnabled(true);
...
}
}
}
Witch loops the entire component and checks to see if this matches the instance JButtonand reset to the default values.
It is good that you can use many components, considering that you want to reset also JTextFields or any components that you can use:
private void clearAllFields() {
for (Component C : myPanel.getComponents()) {
if (C instanceof JButton || C instanceof JTextField) {
((JTextField) C).setText("");
...
}
if (C instanceof JButton) {
((JButton) C).setText("");
((JButton) C).setEnabled(true);
...
}
if (C instanceof JRadioButton) {
((JRadioButton) C).setSelected(false);
...
}
if (C instanceof JDateChooser) {
((JDateChooser) C).setDate(null);
....
}
}
}
YCF_L source
share