I have an array of JComponent objects containing JTextField and JComboBox objects. I chose to make one array instead of two for more efficiency. But if I iterate over the objects I can't seem to cast them correctly, since my IDE doesn't recognize the .setText() method in the last line of the example code. How do I cast the items properly?
JComponent[] items = {JTextField1, JComboBox1};
for (JComponent item : items) {
if (item instanceof JTextField) {
item = (JTextField) item;
item.setText(null);
}
else {
item = (JComboBox) item ;
item.setSelectedIndex(-1);
}
}