Results 1 to 6 of 6
  1. #1
    hturki is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default 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 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.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm not sure what your experience with java is, however; this would definitely require a lot of work and a pretty good understanding of the language. Off the top of my head you would probably need a custom cell renderer, then listen to mouse events and remove items when mouse events occur. You could also have each cell have a button and the button deletes the item. Either way it will probably take a lot of work.

    I believe the easiest approach would be to have a single button next to the JCombo box which removes selected elements. If you need it to look how you described then this approach isn't good, it is certainly easier though.

  3. #3
    hturki is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    Yeah, I need the delete icon/button for every entry. Thanks for trying, though.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Like I said, it's possible but I'd imagine your experience level isn't quite what you need to accomplish it.

    My best suggestion is to make a custom list cell renderer which allows the combo box to hold a j panel, put a label and a button(or image) on the panel.

    You can have an action listener attached to the button(or mouse listener if using an image) which when clicked will remove the item.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Quote Originally Posted by sunde887 View Post
    My best suggestion is to make a custom list cell renderer which allows the combo box to hold a j panel, put a label and a button(or image) on the panel.

    You can have an action listener attached to the button(or mouse listener if using an image) which when clicked will remove the item.
    A renderer is used to render(paint, draw) the view. There's no way for it to receive input events, as it isn't added to the component hierarchy.

    db

Similar Threads

  1. Modify and delete certain xml entries
    By scheilaad in forum XML
    Replies: 2
    Last Post: 02-28-2011, 11:18 PM
  2. Help on Basic table entries....
    By picuser in forum AWT / Swing
    Replies: 2
    Last Post: 12-26-2010, 01:34 AM
  3. Printing multiple user entries.
    By mConfused in forum Advanced Java
    Replies: 2
    Last Post: 04-09-2010, 12:44 PM
  4. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  5. Stoping repeated entries in JComboBox
    By MasterDeveloper in forum AWT / Swing
    Replies: 5
    Last Post: 04-05-2009, 12:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •