Solved

adding new items into JComboBox

Posted on 2004-04-29
12
329 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 62
difference between sorce folder and folder in eclipise 3 29
java mysql insert application 14 29
jsp error 6 26
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

820 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