When inputting a new Student
object inside my JTables I would like to hold all the localities/cities inside my JComboBox. Each city is stored inside a HashMap
which holds the name as key(as there are no duplicates) and it's postcode.
Example: hashMap.put("City", "AAA")
Now my issue would be to represent the HashMap
OR List<String>
inside the JComboBox itself. The cheap and easy alternative was to simply re-write the String[]
to hold all the town names and switch-case
the selected value but there are few issues with that:
- It's too long, adding new localities may be a pain or waste of time
- Alot of unnecessary code
- Looks horrible if being reviewed by someone
- Potentially slower than my proposed method
Here you go :
Actually you can do :
but , for every single elemen of
JComboxBox
will hold all elements of list in a row.*To prevent ambiguity between
java.util.List
&java.awt.List
, we should declare them clearly.Here the complete demo :
And the result of
JComboBox< java.util.List<String>> combo = new JComboBox<>( ); combo.addItem(list);
declaration :