• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JComboBox with deletable entries

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

First of all, seeing as this my first post, please let me apologize in advance if I'm doing something wrong by posting this (although I did browse the web and the forum to see if someone answered this question already and came up with nothing):

I just started working on a project that absolutely needs to have a JComboBox that supports deleting entries. It should look like a regular JComboBox, but with something like a trash icon next to each entry that, once clicked on, would delete the entry, (and ideally fire a specific Event that would allow a listener to know that something's been deleted). From what people told me, this is supposed to be really hard to implement by extending the standard JComboBox class, and someone recommended that I perhaps create a new class from scratch from JComponent (and then have a scrollable JPopupMenu when one clicks on it and so on...). However, it would be a massive pain to make this custom class function like a JComboBox (especially since the Swing source code is really hard to figure out), and to make matters worse, the project I'm working on has a specific look and feel for JComboBoxes that I would have to specifically replicate for this JComponent.

Anyways, since a JComboBox with deletable entries seems like a pretty useful tool that I doubt that I would be the first person to have though off, I was wondering if any of you have encountered this challenge in the past, and what you all would recommend as a course of action.

Thanks in advance, and sorry again in advance if I'm being a n00b.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Haithem, and welcome to the Ranch!

How about a normal JComboBox with an adjacent JButton that removes the selected item? That's what I would go for.

fire a specific Event that would allow a listener to know that something's been deleted


DefaultComboBoxModel.addListDataListener(...)
 
Haithem Turki
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately I need to have a trash icon/button for every entry in the drop-down menu.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's going to be very difficult, as you already know. The default listeners don't provide fine grained control to respond differently if the icon is clicked. And there's a whole lot of package private stuff that would stand in the way of extending existing JDK classes.

I don't see any straightforward way of achieving this behavior with a JComboBox that would be independent of LaF.

Does it have to be a drop-down, or could you possibly use a two-column, uneditable JTable without gridlines, in a JScrollPane (possibly with the header removed), which could give the appearance of a JList?
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, we ask that you BeForthrightWhenCrossPostingToOtherSites

Could you please provide links to your two other cross posts, and any others I may not have seen?
 
Haithem Turki
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad (as I said, I'm new to this game):

http://forums.oracle.com/forums/thread.jspa?messageID=9559476#9559476
http://www.java-forums.org/awt-swing/43412-jcombobox-deletable-entries.html
 
Haithem Turki
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I'm trying to make it as undistinguishable from a normal JComboBox as possible.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic