Friday, 15 May 2015

java - How to add different class types in a JComboBox? -



java - How to add different class types in a JComboBox? -

is possible instantiate object type set in jcombobox @ runtime?

let's have abstract class fruits. have classes apple , banana extending fruits. there reference object gonna instantiated @ runtime: fruits chosenfruit

i want this:

fruits chosenfruit; jcombobox<fruits> combo = new jcombobox<fruits>(); apple applecombo = new apple(); banana bananacombo = new banana(); combo.add(applecombo ); combo.add(bananacombo); // when user chose fruit in jcombobox , clicked ok: chosenfruit = new combo.getselecteditem().getclass(); // or .gettype() ???

the problem here is, instantiated 2 objects (applecombo , bananacombo) add together them jcombobox. want type of new object displayed in jcombobox , set user. hope reasonable.

thanks in advance!

edit: so, there way this:

jcombobox<class> combo = new jcombobox<class>(); combo.add(apple); combo.add(banana); fruits chosenfruit = new combo.getselecteditem();

java swing generics jcombobox

No comments:

Post a Comment