1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
import java.lang.reflect.Field;
import javax.swing.JRadioButton;
public class Test {
private JRadioButton jRadioButtonHomePlayer1 = new JRadioButton("1");
private JRadioButton jRadioButtonHomePlayer2 = new JRadioButton("2");
private JRadioButton jRadioButtonHomePlayer3 = new JRadioButton("3");
private JRadioButton jRadioButtonHomePlayer4 = new JRadioButton("4");
//...
public void setAllToFalse()
{
int nbJradio = 4;
for(int i = 1; i <=nbJradio; i++)
{
Field field;
try {
field = this.getClass().getDeclaredField("jRadioButtonHomePlayer"+i);
JRadioButton jRadioButton = (JRadioButton) field.get(this);
jRadioButton.setVisible(false);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String args[])
{
new Test().setAllToFalse();
}
} |
Partager