setEditable() - Use Combo Box as Text Field

This section provides a tutorial example on how to call the setEditable(true) method to use a combo box as a dropdown list and text field at the same time.

If you want to use a combo box as both a dropdown list and a text text field, you can call the getEditable(true) method on the combo box. Here is a sample program:

/* JComboBoxAsEditable.java
 * Copyright (c) 1997-2024 HerongYang.com. All Rights Reserved.
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JComboBoxAsEditable implements ActionListener {
   JComboBox<String> combo;
   JLabel myLabel = null;
   public static void main(String[] a) {
      JComboBoxAsEditable myTest = new JComboBoxAsEditable();
      myTest.createFrame();
   }
   public void createFrame() {
    JFrame f = new JFrame("My Combo Box");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container c = f.getContentPane();
      c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
    
      JLabel color = new JLabel("Colors:");
      c.add(color);

      String[] options = {"Red", "Green", "Blue"};
      combo = new JComboBox<String>(options);
    combo.setEditable(true);
    combo.setSelectedIndex(-1);
      c.add(combo);
    
      JButton b = new JButton("Done");
      b.addActionListener(this);
      c.add(b);
    
      myLabel = new JLabel("Please end/select");
      c.add(myLabel);
      f.pack();
      f.setVisible(true);
   }
   public void actionPerformed(ActionEvent e) {
    String t = (String) combo.getSelectedItem();
    if (t == null) t = "Nothing entered/selected";
      myLabel.setText(t);
   }
}

If you run this program, you will see none of the options is selected initially and nothing is entered in the text field. If you click "Done", you will get the "Nothing entered/selected" message. If you select an option then click "Done", you will get the selected color. If you enter your own color, and click "Done", you will get the color you entered:

JComboBox Editable as Text Field
JComboBox Editable as Text Field

Table of Contents

 About This Book

 JDK (Java Development Kit)

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JCheckBox - Swing Check Box Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

JComboBox - Swing Combo Box Class

 javax.swing.JComboBox and Related Methods

 ActionListener and ItemListener

 getSelectedItem() - Selected Item of Combo Box

setEditable() - Use Combo Box as Text Field

 Menu Bar, Menus, Menu Items and Listeners

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker

 AWT (Abstract Windows Toolkit)

 Integration with Desktop System

 Archived Tutorials

 References

 Full Version in PDF/EPUB