This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes Removing duplicate substrings from Jcombobox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Removing duplicate substrings from Jcombobox " Watch "Removing duplicate substrings from Jcombobox " New topic

Removing duplicate substrings from Jcombobox

John Towers

Joined: Jan 18, 2007
Posts: 4

I�m working on a program that displays XML in comboboxes. I�m reading the source document into string. Then taking the contents of part of each line of this document, specifically that contained in �<� and �>� tags, through substrings and adding the contents of these substrings in a combobox, to represent XML nodes. And it works! Or at least, seems to.

The problem is though, that I can�t figure out how to remove duplicate values from the combobox when using substings, so I'm getting a lot of repeated elements.

Can anyone tell me of away to do this so that the in the combobox remain unique? If anyone could help me here that would be great, I�m not getting very far on my own.


[ January 18, 2007: Message edited by: John Towers ]
[ January 18, 2007: Message edited by: John Towers ]
Henrik Engert
Ranch Hand

Joined: Apr 26, 2005
Posts: 70
One way of doing it is to store the values in a collection or something like that. Then before adding the new value check if it already exists in the collection.

For example a HashSet can be used to add the specified element to this set if it is not already present.

Or a simple ArrayList. ArrayList has a method called contains(Object obj) that will tell you if the value is added or not.

Nick Lombard

Joined: Nov 04, 2012
Posts: 1
A custom ComboBoxModel should do the trick, maybe something like:

Don't get me started about those stupid light bulbs.
subject: Removing duplicate substrings from Jcombobox