Solved

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

Posted on 2004-04-11
2
212 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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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 …

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now