質問

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();
}
役に立ちましたか?

解決

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

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top