0

I have a JComboBox. I would like it to work so that if a certain item is selected ("Other"), immediately, several more items are displayed in the same combo box (something like a submenu, but inside the combo box). I'm having a devil of a time getting this to work.

Does anyone have any ideas on how to do this?

Steve Cohen
  • 4,679
  • 9
  • 51
  • 89
  • It would be helpful if you showed us what have you tried. in the meantime, take a look at that: http://stackoverflow.com/questions/58939/jcombobox-selection-change-listener – MByD Nov 08 '12 at 16:10
  • 1
    Why don't you put tree into JCombobox? I think it may satisfy your demand. – Thinhbk Nov 08 '12 at 16:11
  • 1
    You'd have better luck with a JTree, but basically, you'd have to insert the new items into the DefaultComboBoxModel as a part of the selection listener of the JComboBox. – Gilbert Le Blanc Nov 08 '12 at 16:13
  • @Thinhbk: please elaborate on your idea of putting tree into JComboBox. – Steve Cohen Nov 08 '12 at 16:14
  • 1
    @SteveCohen: take a look at this implementation: http://www.jroller.com/santhosh/entry/tree_inside_jcombobox – Thinhbk Nov 08 '12 at 16:15
  • IMO, the idea to put JTree into JCombobox is obvious, but to handle it correctly is actually a tough job. – Thinhbk Nov 08 '12 at 16:20
  • I haven't worked with JTrees before and am reluctant to start now. I am making some progress with using an ItemListener. The event I want to capture is the Item Selection Event, rather than the action event. – Steve Cohen Nov 08 '12 at 17:10

1 Answers1

2

EDIT: misunderstood your question!

I assume that you are clicking on an item in the JComboBox? Than simply add this code

comboOther.addActionListener (new ActionListener () {
        public void actionPerformed(ActionEvent e) {
            comboOther.addItem("new item 1");
            comboOther.addItem("new item 2");
            comboOther.addItem("new item 3");
            // more
        }
});
Captain Obvious
  • 745
  • 3
  • 17
  • 39