Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

adding new items into JComboBox

Posted on 2004-04-29
12
Medium Priority
?
337 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 800 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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 800 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

661 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