Solved

Is there a way to add entries into a combobox and check they are not repeated?

Posted on 2004-04-11
2
243 Views
Last Modified: 2010-03-31
That's actually my question. I'm using Netbeans 3.6, thanks
0
Comment
Question by:hacktek
  • 2
2 Comments
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10803684
You have to extend the "javax.swing.DefaultComboBoxModel" and have to write your custom model.

in the addElement method, you have to check for the element's existence, if it is not present,
then you can add the element.


Otherwise, you can ignore the element.


Regards,
Muruga
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 35 total points
ID: 10803708
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;

public class UniqueElementComboBoxModel extends DefaultComboBoxModel
{
      public UniqueElementComboBoxModel()
      {
            super();
      }

      public UniqueElementComboBoxModel(Object items[])
      {
            this();

            int i, c;
            for (i = 0, c = items.length; i < c; i++)
                  addElement(items[i]);

            if (getSize() > 0)
            {
                  setSelectedItem(getElementAt(0));
            }
      }

      public UniqueElementComboBoxModel(Vector v)
      {
            this();

            int size = v.size();

            for (int i = 0; i < size; i++)
            {
                  addElement(v.get(i));
            }

            if (getSize() > 0)
            {
                  setSelectedItem(getElementAt(0));
            }
      }

      public void addElement(Object anObject)
      {
            if (getIndexOf(anObject) < 0)
            {
                  super.addElement(anObject);
            }
      }

      public void insertElementAt(Object anObject, int index)
      {
            if (getIndexOf(anObject) < 0)
            {
                  super.insertElementAt(anObject, index);
            }
      }
}


Here is the model, that maintains the uniqueness of the data.


Regards,
Muruga
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 45
swing controls 2 22
DTD and JAVA versions 1 30
Java Inheritance super keyword use 8 36
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question