Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox with deletable entries  RSS feed

 
Haithem Turki
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
 
Haithem Turki
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I'm trying to make it as undistinguishable from a normal JComboBox as possible.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!