Solved

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

Posted on 2004-04-11
2
250 Views
Last Modified: 2010-03-31
That's actually my question. I'm using Netbeans 3.6, thanks
0
Comment
Question by:hacktek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

695 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