Solved

adding new items into JComboBox

Posted on 2004-04-29
12
328 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
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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 92
hibernate example using maven 12 42
by zero exception 10 41
Convert from a json string array to a Java object 3 35
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

822 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