?
Solved

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

Posted on 2004-04-11
2
Medium Priority
?
253 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 140 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

Technology Partners: 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 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