0

Is it possible to instantiate an object whose type is set in a JComboBox at runtime?

Let's say I have an abstract class Fruits. I have classes Apple and Banana extending Fruits. There is a reference to an object that is gonna instantiated at runtime: Fruits chosenFruit

I want to do something like 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 a fruit in JComboBox and clicked OK:
chosenFruit = new combo.getSelectedItem().getClass(); // or .getType() ???

The problem here is, that I instantiated two objects (appleCombo and bananaCombo) only to add them to the JComboBox. But I want only the type of the new object to get displayed in JComboBox and to get set by the user. I hope this is reasonable.

Thanks in advance!

Edit: So, is there a way to do something like this:

JComboBox<Class> combo = new JComboBox<Class>();
combo.add(Apple);
combo.add(Banana);
Fruits chosenFruit = new combo.getSelectedItem();
mKorbel
  • 109,525
  • 20
  • 134
  • 319
fachexot
  • 491
  • 1
  • 8
  • 24
  • 1
    I'm not sure where you're stuck. Is your problem that your objects don't render correctly in the JComboBox? If so, have you written a custom renderer? Why not give Fruits a method, `getDisplayString()` and then have the renderer call it, displaying whatever you wish it to display. – Hovercraft Full Of Eels Feb 17 '13 at 01:54
  • This is not the problem, I know I could overwrite toString(). But I do not want to instantiate an object for each item in the JComboBox, because they won't get used. I only want to instantiate the object chosenFruit, after a type was selected in JComboBox. – fachexot Feb 17 '13 at 01:56
  • 2
    Then perhaps you want to fill the combo box not with Fruit objects but with FruitType enums or somesuch, and use the enum returned to allow you to create the correct Fruit object, perhaps using a static factory method. – Hovercraft Full Of Eels Feb 17 '13 at 01:57
  • Jeah, this is what I want. Thank you! I will look out for advice implementing such a static factory, but I think, this is it. Thanks. – fachexot Feb 17 '13 at 02:00
  • Honestly, I am reading Head First Design Patterns currently, and my bookmarker is exactly at page 109 (cover page of chapter 4) ;-) Should have read one page further. – fachexot Feb 17 '13 at 02:18
  • I think that for a factory to be a "design pattern" it must be an abstract factory, but I'm not sure that you need that here, but you could use it if you wanted to experiment with the concept. – Hovercraft Full Of Eels Feb 17 '13 at 02:20
  • So I only create an enum, fill the JComboBox with the enum's values and implement one static method that returns the created object? – fachexot Feb 17 '13 at 02:31
  • Perhaps. Give it a try. – Hovercraft Full Of Eels Feb 17 '13 at 03:00
  • 1
    [`enum Hue`](http://stackoverflow.com/a/14887457/230513) is an example. – trashgod Feb 17 '13 at 07:31

0 Answers0