Solved

adding new items into JComboBox

Posted on 2004-04-29
12
334 Views
Last Modified: 2010-07-27
Hi,

I have a JComboBox already.  And now I would like make this JComboBox bigger, more choices, if some flags are set.  How can I do this?

Here is my codes...
String ComboBoxStr[] = { "a1", "a2", "a3", "a4", "a5"};
JComboBox aComboBox = new JComboBox(ComboBoxStr);
Boolean setFlag = false;

if (setFlag == true)
{
  // let say if I want to have 5 more choices like this...ComboBoxStr[] = { "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10"};

  // so what can I do here???
}
0
Comment
Question by:xenia27
[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
  • 5
  • 4
  • 3
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10955922
aComboBox.addItem(newitem);
0
 

Author Comment

by:xenia27
ID: 10955929
should I have a new String array for "newitem"??  Or just simply put a string??
0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 10955952
you can just add the new items. eg.
aComboBox.addItem("a6");
aComboBox.addItem("a7");
aComboBox.addItem("a8");
aComboBox.addItem("a9");
aComboBox.addItem("a10");
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 92

Expert Comment

by:objects
ID: 10955956
or you could use a loop if you already have an array
0
 

Author Comment

by:xenia27
ID: 10956328
If I do have an ActionListener when I first initialized aComboBox and now I add some new items...will the new items also ahve action listener???
0
 
LVL 92

Expert Comment

by:objects
ID: 10956389
yes
0
 

Author Comment

by:xenia27
ID: 10956397
so that means I don't need to add ActionListener to the new items??  Ok...so if I want to remove the new item...I need to use "removeItem"??

How can I know whether the new item I wanna add is existing in a JComboBox so I won't add another one again??
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10956998
>> so that means I don't need to add ActionListener to the new items??  
The actionlistener listens to actions happening on *the combobox* no matter how many items are in it

>> if I want to remove the new item
To remove you can use
            void removeItem(Object anObject)
or
            void removeItemAt(int anIndex)

For the first you have to pass the object that is in it
For the first you only need to pass the index

>>How can I know whether the new item I wanna add is existing in a JComboBox so I won't add another one again??
Whenever you add an item to it or remove an item from it, you could also add/remove it from a Set.
Then before adding you could look if it exists in the set. If so, you shouldn't add it anymore.
(The Set is for not having to interrogate each item of the Combobox to check if the item you want to insert is already in)





0
 
LVL 37

Accepted Solution

by:
zzynx earned 200 total points
ID: 10957022
Avoiding duplicates:

Set theSet = new HashSet()

1) Adding to the combo box should be like this:

instead of

      yourCombo.add("a1");

you should write:

      if ( !theSet.contains("a1") ) {
           yourCombo.add("a1");
           theSet.add("a1")
      }

2) Removing
instead of

      yourCombo.removeItem("a1");

you should write:

      yourCombo.removeItem("a1");
      theSet.remove("a1");
0
 

Author Comment

by:xenia27
ID: 10957550
Great!  I got it work out perfectly!!!  Thanks a lot!!!!!!!!!!!!!  ^^
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10957672
Glad for you!
Thanks for accepting xenia.
Till the next
0
 
LVL 92

Expert Comment

by:objects
ID: 10964906
:)
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

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

636 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