How to Add an Item to JComboBox

A JComboBox is a scrollable list of options or labels in a Java graphical user interface, or GUI, program. You cannot edit text in this box; however, you can select multiple items from the list of options. Each item in a JComboBox is defined within the Java code, and additional items can be pushed into the box with a single line addition. These combination boxes are applied in many different programs, such as when selecting a set of interests or the days of the week when an alarm should ring.

Instructions

    • 1

      Open your Java application in your preferred text editor or Java development environment.

    • 2

      Locate your JComboBox within the program. All JComboBox items are created with the "New JComboBox" syntax, so you can use your program's "Search" feature to scan the document for all occurrences of the word "JComboBox."

    • 3

      Add the new item to the JComboBox on a new line using the "addItem()" function of the JComboBox. This command must be issued after the JComboBox is created. For example, to add the text "Banana" and "Elephant" to a JComboBox named "combo1," you would enter the following code:

      JComboBox combo1 = new JComboBox();

      combo1.addItem("Banana");

      combo.addItem("Elephant");

Related Searches

References

You May Also Like

Related Ads

Featured
View Mobile Site