Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Swing / AWT / SWT and the fly likes Editable JComboBox w. JDialogBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Editable JComboBox w. JDialogBox" Watch "Editable JComboBox w. JDialogBox" New topic

Editable JComboBox w. JDialogBox

Charles Van Damme

Joined: Feb 24, 2009
Posts: 19
Good day

I need a JComboBox or JList or JTable object where I can:
- select an item by the usual simple left click (default behaviour of JCombobox)
- get a dialog box by double clicking (or shift click or any else). This dialog box enables to:
- suppress an existing item
- add a space before an existing item, and then edit it (JTestField in a JDialogBox or JOptionPane)
- shift an item up or down in the list (Mutable items)

Anything I think I'm certainly not the only one to desire, is it ?

Could you give us some useful hints ? E.g.
- No JCombobox, rather JList because ...
- Need to use a data model, a CellRenderer, a CellEditor, an EventHandler ...
- regarding the event handler
- possible stumbling blocks
- URL of site to find code example in this direction

I think it would not succeed with a JComboBox because when attempting to double-click an item, as soon as the first click occured, the selection event handler operates and the box closes, leaving no time for a second click.
JList doesn't offer cell edition capabilities on the contrary of a JTable cell. That would not be an issue as the edition may occur in a JTestField of a JDialogBox or JOptionPane.

Thanks for any good intention ...
And thanks to Big Moose Saloon and their subscribers for the efficient tips that I got before ...
Ranganathan Kaliyur Mannar

Joined: Oct 16, 2003
Posts: 1101

Yes, JList suits your situation better than JComboBox. You can also think of using a JTable with just one column - in this case, you can edit the data then and there.
As for JList, you can read the tutorial or check the api.
You should look to use a ListSelectionListener or MouseListener.

Darryl Burke

Joined: May 03, 2008
Posts: 5115

I might go for an editable JComboBox, with a key binding (possibly Alt-Delete or Ctrl-Delete) added to remove the currently selected item. JComboBox being a composite component, the binding would likely have to be added to the WHEN_ANCESTOR_OF_FOCUSED_COMPONENT map.

edit Just re-read the OP.
- shift an item up or down in the list (Mutable items)

A single column JTable does sound more suitable for that. Reordering in a combo doesn't suggest any user-friendly approach.

luck, db
There are no new questions, but there may be new answers.
Charles Van Damme

Joined: Feb 24, 2009
Posts: 19
Thanks Ranganathan Kaliyur Mannar and Darryl Burke.

After thinking longly and looking attentively to all the methods of the JComboBox, JList and JTabel API's, a s o, I decided although to make a first a trial based on JComboBox.
Hence my next post "Preventing the unrolled list of a JComboBox to close".

Thanks again.
I agree. Here's the link:
subject: Editable JComboBox w. JDialogBox
It's not a secret anymore!