I'm coding an option's pannel, and to be able to add more options faster while I develop the application, I've decided to get all the input's components's in a Frame, I need to load their values from the config and set the corresponding text but it seems that it can't grab the component's text from the field.
I'm getting a:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.awt.Component.setText
Nombre: server Clase:class javax.swing.JTextField
private void loadConfigs() {
List<Component> compList = getAllComponents(this);
System.out.println("Tamaño "+compList.size());
for(int i=0; i<compList.size();i++) {
if(compList.get(i).getName() != null) {
System.out.println("Nombre: "+compList.get(i).getName() +" Clase:"+ compList.get(i).getClass().toString());
if(compList.get(i).getClass().toString().matches("class javax.swing.JTextField")) {
System.out.println("Machea load " +compList.get(i).getName() + compList.get(i).toString());
compList.get(i).setText(rootFrame.config.get(compList.get(i).getName()));
}
else if(compList.get(i).getClass().toString().matches("class javax.swing.JCheckBox")) {
if (rootFrame.config.get(compList.get(i).getName()) == null) {
compList.get(i).setSelected(false);
}
else {
compList.get(i).setSelected(true);
}
}
}
}
}
public static List<Component> getAllComponents(final Container c) {
Component[] comps = c.getComponents();
List<Component> compList = new ArrayList<Component>();
for (Component comp : comps) {
compList.add(comp);
if (comp instanceof Container) {
compList.addAll(getAllComponents((Container) comp));
}
}
return compList;
}
Here:
compList.get(i).setText(....)
The compiler only sees compList.get(i)
as a Component. To use JTextField methods you must first cast this as a JTextField.
((JTextField)compList.get(i)).setText(....)
Your plan here seems to me to be kludgy and very non-OOPs compliant though.
Perhaps you want to create a Map<String, JTextField>
and give the class a public method to get the Strings held by the text fields associated with the String that represents what the JTextField represents.
You should be using something like this:
if(compList.get(i) instanceof JTextField) {
JTextField field = (JTextField) compList.get(i);
field.getText(); // etc
}
instead of the getClass().toString
checks you've been doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With