Java Swing Tutorial - Java JComboBox.setEditable(boolean aFlag)








Syntax

JComboBox.setEditable(boolean aFlag) has the following syntax.

public void setEditable(boolean aFlag)

Example

In the following code shows how to use JComboBox.setEditable(boolean aFlag) method.

/*from ww w .  j  av a  2 s .co m*/
import java.awt.BorderLayout;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

  public static void main(final String args[]) {
    String labels[] = { "A", "B", "C", "D", "E" };
    final DefaultComboBoxModel model = new DefaultComboBoxModel(labels);

    JFrame frame = new JFrame("Shared Data");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    JComboBox comboBox1 = new JComboBox(model);
    comboBox1.setMaximumRowCount(5);
    comboBox1.setEditable(true);

    JComboBox comboBox2 = new JComboBox(model);
    comboBox2.setMaximumRowCount(5);
    comboBox2.setEditable(true);
    panel.add(comboBox1);
    panel.add(comboBox2);
    frame.add(panel, BorderLayout.NORTH);


    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}