0

I'm working on a program on which 2 JComboBoxes must set each other's selected item. Let's dub their object names wheelsTypeCombo and carBrandCombo. Both of their models contain each a list of objects retrieved from a database(objects of type 1 in the first list and objects of type 2 in the second list)and also a special wheelsTypeShowAll and respectively carBrandShowAll of the corresponding types. When either of the last 2 items are selected then all the containing items in both JComboBoxes are shown including themselves(the 2 special items).

The rules of the JComboBoxes reciprocal relation are as follows:

  1. when we select an item from wheelsTypeCombo, the carBrandCombo is reloaded with all the car brands that use that ONLY those types of tires and the carBrandShowAll item
  2. when carBrandShowAll item is selected from the 1st situation state of carBrandCombo then the carBrandCombo is reloaded with all the corresponding items from the database, also the wheelsTypeCombo has the selected item wheelsTypeShowAll .
  3. when we select an item from carBrandCombo then the corresponding wheels type item is selected in the wheelsTypeCombo
  4. if items that are already in a corresponding relation are selected then no setSelectedItem action is required by either of the JComboBoxes

Momentarily i tried using anonymous ItemListener classes, when I add the JComboBoxes to the interface, but found myself battling stack and null pointer exceptions overflows caused by inter calling method calls probably.

I would appreciate any kind of reasonable improvements on any level.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

1

If I were going to do this, I'd create two custom classes, carBrand and wheelsType, then have each of them implement a getUsers call which returns their own internal List of items that are relevant:

public carBrand
    public static Enum allWheels
    List<wheelsType> myWheels;
    getWheels() {
        return myWheels;
    }
    setWheels();

public wheelsType
    public static Enum allBrands
    List<carBrands> myBrands;
    getBrands();
    setBrands();

Then in your comboBox code, hold a list of your wheels and brands, then update the Listmodel with the results of

getSelected().getBrands()

or

getSelected().getWheels().

You could also then have a static call to getAllBrands() or whatever for when they choose your 'all' option.

There are probably 'simpler' ways, like making a Map for each, but I think this would be the most 'clear' way.

Charles
  • 245
  • 2
  • 11