Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

JComboBox (uneditable)

JavaFAQ Home » Java Notes by Fred Swartz Go to all tips in Java Notes by Fred Swartz

Bookmark and Share

JComboBox (uneditable)

Making Choices

There are several ways to select one of many fixed choices: radio buttons, a menu, a list, or a (uneditable) combo box. A combo box (JComboBox) is a popup menu and is often the best component to use because it uses less space than radio buttons or a list, and it shows as part of a "form" so the user realizes it's there, unlike a menu.

You can also use an editable combo box, and in this case the user can either choose from the pop-up menu or type in a value.


An easy way to build a combo box is to initialize an array (or Vector, but not yet the new Collections types) of strings and pass it to the constructor. Objects other than strings can be used, but this is the most common. The list also be built by successive calls to the add method.
<pre class="example">String[] dias = {"lunes", "martes", "miercoles"};
JComboBox dayChoice = new JComboBox(dias)</pre>

Common methods

In these prototypes, assume the following.
<pre>   int index;
   String item;
   String obj;  // obj can be any object type, but is commonly String
   JComboBox cb = new JComboBox(. . .);
Result Call Description
  cb.addActionListener(...); Add listener -- same as for button.
Modifying a JComboBox
  cb.setSelectedIndex(index); Set default, visible, choice.
  cb.setSelectedItem(obj); Set default, visible, choice.
  cb.add(item); Add additional item to the list
  cb.insert(item, index); Add additional item after index.
  cb.remove(item); Remove item from the list
  cb.remove(index); Remove item as position index
Testing a JComboBox
index = cb.getSelectedIndex(); Return index of selected item.
obj = cb.getSelectedItem(); Return selected item.


A JComboBox generates both ActionEvents (like buttons, text fields, etc), or ItemEvents. Because it is easier to work with ActionEvents, we'll ignore the ItemEvents. When the user chooses an item from a combo box, an ActionEvent is generated, and the listener's actionPerformed method is called. Use either getSelectedIndex to get the integer index of the item that was selected, or getSelectedItem to get the value (eg, a String) that was selected.


This example creates a JComboBox, and adds a listener to it. The getSelectedItem method returns an Object type, so it's necessary to downcast it back to a String.
<pre class="example">String[] fnt = {"Serif", "SansSerif", "Monospaced"};
JComboBox fontChoice = new JComboBox(fnt);

    new ActionListener() {
        public void actionPerformed(ActionEvent e) {   
            JComboBox combo = (JComboBox)e.getSource();
            currentFont = (String)combo.getSelectedItem();
<div class="footer">
   Copyleft <a target="_blank" href="http://www.leepoint.net/fred/">Fred Swartz</a>
   <a target="_blank" href="http://www.opensource.org/licenses/mit-license.php">
   MIT License</a></div>

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy