Reputation: 931
I have multiple JComboBox elements located across 4 tabbed panes. I would like to be able to encapsulate them into an Object[] and call removeAllItems()
. However, being that it's of type Object I can't do this. Is there a way to put JComboBox elements inside of an array and still access JComboBox methods?
(I've illustrated what I would like to do below)
Example:
Object[] combo_container = { winners_combo_box, bikes_combo_box,
teams_combo_box, riders_combo_box };
for(Object item : combo_container) {
item.removeAllItems();
}
Upvotes: 0
Views: 61
Reputation: 671
for(Object item : combo_container) {
JComboBox box = (JComboBox) item;
box.removeAllItems();
}
But is not really a great idea. Why not array of JComboBox? Like this:
JComboBox<String>[] combo_container = { winners_combo_box, bikes_combo_box,
teams_combo_box, riders_combo_box };
for(JComboBox box : combo_container)
box.removeAllItems();
Of course assuming winners_combo_box, bikes_combo_box etc. are objects of JComboBox class
Upvotes: 1
Reputation: 7769
for(Object item : combo_container) {
JComboBox tmp = (JComboBox) item;
tmp.removeAllItems();
}
You can still create JComboBox array and put all the element in it. The warning is nothing but telling you that JComboBox is a generic class so it expects <TYPE>
. But that's fine, ignore it
Upvotes: 1