0

I've created a JComboBox and when I choose an option from the combo box I want to add another combo box (for choosing more specific details) for the selected option. Is this possible?

I've tried creating another JComboBox in the original combo box's actionPerformed class, but I didn't solve the problem.

The result should look something like this:

double columned combobox

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tyler Joe
  • 357
  • 2
  • 18
  • 1
    First of all please provide a [mcve], so we can better understand what you've tried so far and can provide a solution that is suitable for your case. Please also tell us what you want to achieve. The image, you've posted is not self-described for me. – Sergiy Medvynskyy Jul 18 '19 at 09:21
  • 2
    That looks more like it should be a `JTree` or nested `JMenu` controls (a menu can contain menu items and/or other menus). – Andrew Thompson Jul 18 '19 at 10:15
  • 2
    Have you ever, in any application or on any website, seen a nested combo box? Probably lot. Stick to the conventions, otherwise your end users might not be so happy about that. A `JMenu` with sub-menus might be an option. But alternatively, you could have a second (initially disabled) combo box (with the numbers), and after the selection in the first combo box (with the names) was made, the second combo box is enabled and filled with the proper numbers for the selected name. – Marco13 Jul 18 '19 at 10:40
  • I don't see the point of what you are suggesting. You don't have duplicates of "Alice Brown" where you can select a value with 10 or 11 or 12. So how is this choosing more specific details? – camickr Jul 18 '19 at 14:00
  • How about something like: https://stackoverflow.com/questions/4982260/binding-comboboxes-in-swing/4982576#4982576. It uses multiple combo boxes. The options in the second change when a selection is made in the first. – camickr Jul 18 '19 at 14:01

1 Answers1

1

Regarding adding a new JComboBox: Rather than actionPerformed try with JComboBox.addItemListener(ItemEvent). This is the right place to get the selected item and therefore to create and populate a new JComboBox with details you want to include. Here a link to a demo project in GitHub