|
-
May 2nd, 2011, 11:35 PM
#1
JComboBox with deletable entries
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 of having something like this for a project, 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.
-
May 3rd, 2011, 01:42 PM
#2
Re: JComboBox with deletable entries
-
May 3rd, 2011, 03:02 PM
#3
Re: JComboBox with deletable entries
-
May 3rd, 2011, 07:52 PM
#4
Re: JComboBox with deletable entries
Cross-posted without notification on Java-Forums, JavaRanch, and OTN Discussion Forums 
I'm out
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
May 4th, 2011, 12:52 PM
#5
Re: JComboBox with deletable entries
There is a way of doing this although due to the lack of access to the JCombox internals it requires a bodge job.
The annoying thing is I spent 40 minutes last night checking my theory and writing a response only for the site to refuse to accept my submission and then wipe the editor panel so I couldn't even save the contents of my post. I had no time to rewrite everything last night and I'm now glad I didn't given this question is posted on virtually every other forum.
The OP has also admitted to cross posting on the other sites but not all of them have references to this site so I won't waste my time and others.
-
May 5th, 2011, 01:30 AM
#6
Re: JComboBox with deletable entries
Without having to sink into the underlying depths of how rendered components are assembled, you might consider this....
You can have mouse listener in the comboboxes popup panel, which will tell you when mouse is clicked. You can have the trash icon in the rendered items, you would know the width of the icon. So, it would be a fairly simple calculation to know if the mouse click occured on top of the trash icon. Hence, in such a case, asking for the comboboxes selected row, you can initiate removal from the model, which in itself fires an event.
What I am not sure of, is weather mouse event would ever occur. Perhaps the popup panel is disposed upon selection before it has a chance of handling the mouse event... havent tried it, but it might work.
-
May 5th, 2011, 10:13 AM
#7
Re: JComboBox with deletable entries
 Originally Posted by Londbrok
You can have mouse listener in the comboboxes popup panel, which will tell you when mouse is clicked. You can have the trash icon in the rendered items, you would know the width of the icon. So, it would be a fairly simple calculation to know if the mouse click occured on top of the trash icon. Hence, in such a case, asking for the comboboxes selected row, you can initiate removal from the model, which in itself fires an event.
Yes you're on the right track. The difficulty is in getting access to the popup panel object so you can add your mouse listener.
I added my own renderer to render the items on the popup complete with a trash can icon on each item. This renderer is also used to render the normal combox view so if you only want the trash can to appear on the popup menu view need to check the index passed into the renderer and return the appropriate component. An index of -1 means it is rendering the normal view ie not the popup.
A reference to the popup menu object is passed into the renderer so you can use this to add your mouseListener. The JComboBox with Windows L&F on Windows XP (it may or may not be the same for other L&Fs and or platforms) uses the same popup panel object each time it displays the popup, so in this case you only need to add the mouseListener the first time the renderer is used.
-
May 6th, 2011, 01:46 AM
#8
Re: JComboBox with deletable entries
Well, there is an option to add a PopUpMenuListener to the combobox, events of that are likely to have the popup panel as a source.... no, just checked it, source is the combobox. Theoretically you can create an UI extension where popup is made accessible, it stays as the same object (as in not re-created on show and disposed on hide). Extending the UI approach is here and there used, but this would not be compatible with other UI's. So you would be needed to enforce its usage, or write similar overriding instance to any other expected UIs too.
So I would vote for Keang's approach of attaching listeners in the renderer. Just make sure to do it only once.
Last edited by Londbrok; May 6th, 2011 at 01:52 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|