I'm writing a GUI application. The user selects item from a comboBox (non editable). The suitable input data control is then enabled and user enters data. Then the appropriate object according to the comboBox selected item is created.

  1. I've thought of using String[] array to display the list of choices and then using conditional statements to create the object.
  2. Or create all the objects before user selects and set each object's properties accordingly after user data input.

I can't think of any better way to do this so I'd like to ask if there is one. If no, the first or the second way listed above is better? Thank you.

  • You give too little information. Please add more details.
    – superM
    Commented Jan 8, 2013 at 10:59
  • User selects vehicle type from the combo box list. Then the appropriate data input control is displayed where user enters details for the selected vehicle so that a new vehicle of the selected type is created. Different vehicle types have different details so I have different data input controls for each one. Since the data is necessary for the vehicle object to be created , I thought of either instantiating all the possible vehicles, add them to the combo list and then set their data according to the user data or displaying an array of their names and wait for data input to instantiate them. Commented Jan 8, 2013 at 11:02

1 Answer 1


You could use the abstract factory pattern. For example, provide a list of subclasses of an abstract class VehicleFactory (say CarFactory, BicycleFactory, ...) . Create one object of each concrete factory class and associate the object with the combo box entries. After the user has chosen the entry, use the factory to create the particular Car, Bicycle ... object.

The factories should provide all the meta-information you need to control the GUI correctly, for example, the display name of each object, or which attributes the user has to enter for the particular object. The difference to what you wrote above in your comment is that you don't associate Vehicle objects directly with the combo box, but only factories.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.