1

Hello I am looking for a way to disable a radio buttons if textfield is empty. My radio buttons called buttonGroup1(jbutton1,jbutton2,jbutton3,jbutton4) should be disabled if textfield is empty but I have no idea how to make if statment for this

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2121038
  • 153
  • 1
  • 3
  • 14

1 Answers1

4

A simple solution would be to place the buttons in an array...

JRadioButton[] buttons = new JRadioButton[]{jbutton1,jbutton2,jbutton3,jbutton4};

Determine the state you want the buttons to become...

boolean enabled = !textfield.getText().trim().isEmpty();

The iterate the array and change the state of the buttons...

for (JRadioButton btn : buttons) {
    btn.setEnabled(enabled);
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366