I working in Java Swing and I am generating a dynamic form with control names opc1_1, opc1_2, opc1_3, opc2_1, opc2_2, etc.
How Do I get the value of each one of controls dynamically?
I put a very bad example for illustration
for(int i = 1; i < 10; i ++) {
Control objControl = get("opc1_" + i);
if(objControl == JComboBox)
System.out.println(objControl.getSelectedItem().toString());
else if(objControl == JTextField)
System.out.println(objControl.getText);
}
Thanks so much
Use an array/list to store your
Controlobjects, the names of yourControlobjects in your code aren’t important.Then just iterate through your array/list like you would for any other array/list to get the value of each one of your
Controlobjects.Also,
=is for assignment,==is for comparison, andinstanceofis used for checking if an object is a specific type. You generally try and avoid using theinstanceofoperator in good OO design.