Solved

adding new items into JComboBox

Posted on 2004-04-29
12
326 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
  • 5
  • 4
  • 3
12 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
aComboBox.addItem(newitem);
0
 

Author Comment

by:xenia27
Comment Utility
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
Comment Utility
you can just add the new items. eg.
aComboBox.addItem("a6");
aComboBox.addItem("a7");
aComboBox.addItem("a8");
aComboBox.addItem("a9");
aComboBox.addItem("a10");
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
or you could use a loop if you already have an array
0
 

Author Comment

by:xenia27
Comment Utility
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
Comment Utility
yes
0
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!

 

Author Comment

by:xenia27
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
Great!  I got it work out perfectly!!!  Thanks a lot!!!!!!!!!!!!!  ^^
0
 
LVL 37

Expert Comment

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

Expert Comment

by:objects
Comment Utility
:)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 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

9 Experts available now in Live!

Get 1:1 Help Now