CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2011
    Posts
    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.

  2. #2
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: JComboBox with deletable entries


  3. #3
    Join Date
    May 2011
    Posts
    1

    Re: JComboBox with deletable entries

    Oh yeah, and just FYI, I was an idiot and cross-posted this on other forums (as I said I'm new to this game). The links are here:

    http://www.coderanch.com/t/536533/GU...etable-entries
    http://www.java-forums.org/awt-swing...e-entries.html
    http://forums.oracle.com/forums/thre...559476#9559476

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Apr 2007
    Posts
    442

    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.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JComboBox with deletable entries

    Quote 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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Apr 2007
    Posts
    442

    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
  •  





Click Here to Expand Forum to Full Width

Featured