As you can see below, I have a method which executes statements based on the first letter of a component firing an ItemEvent
:
public void itemStateChanged(ItemEvent ie) {
if(ie.getSource() == rRadioButton) {
currentScale = "r";
((DefaultEditor) rSpinner.getEditor()).getTextField().requestFocus();
}
else if(ie.getSource() == gRadioButton) {
currentScale = "g";
((DefaultEditor) gSpinner.getEditor()).getTextField().requestFocus();
}
else if(ie.getSource() == bRadioButton) {
currentScale = "b";
((DefaultEditor) bSpinner.getEditor()).getTextField().requestFocus();
}
//...
}
Is it possible to easily shorten the method and make it more automatic?
If the argument was an ActionEvent
I could use getActionCommand
to assign the value for currentScale
but the same number of lines of code would be needed first to setActionCommand
for every component.
I could also use setName
and getName
for components but this will result it the same issue as described above.
Also it would be good to be able to automatically point to a specific Spinner based on a specific RadioButton as you can see in the attached source code.
Maybe there is a possibility to use reflection for this but I don't know if this could work.