显示JComboBox的不可选默认值

时间:2013-05-21 08:46:02

标签: java swing jcombobox prompt listcellrenderer

我的JComboBox包含三个{"Personel", "Magasinier", "Fournisseur"}项。

我希望此JComboBox显示值"Choisir une option :",这是一个不可选择的值。

我在initComponents();之后尝试了这段代码:

this.jComboBox1.setSelectedItem("Choisir une option :");

但它不起作用。

我该怎么做?

3 个答案:

答案 0 :(得分:4)

您可以使用以下SSCCE等代码覆盖JComboBox模型中的选择代码:

public class JComboExample {

  private static JFrame frame = new JFrame();
  private static final String NOT_SELECTABLE_OPTION = " - Select an Option - ";
  private static final String NORMAL_OPTION = "Normal Option";

  public static void main(String[] args) throws Exception {
    JComboBox<String> comboBox = new JComboBox<String>();

    comboBox.setModel(new DefaultComboBoxModel<String>() {
      private static final long serialVersionUID = 1L;
      boolean selectionAllowed = true;

      @Override
      public void setSelectedItem(Object anObject) {
        if (!NOT_SELECTABLE_OPTION.equals(anObject)) {
          super.setSelectedItem(anObject);
        } else if (selectionAllowed) {
          // Allow this just once
          selectionAllowed = false;
          super.setSelectedItem(anObject);
        }
      }
    });

    comboBox.addItem(NOT_SELECTABLE_OPTION);
    comboBox.addItem(NORMAL_OPTION);

    frame.add(comboBox);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        frame.setVisible(true);
      }
    });
  }
}

这将显示一个包含初始选择“- Select an Option -”的组合框。用户选择其他选项后,将无法再次选择原始选项。

答案 1 :(得分:2)

我偶然发现了这个问题并对邓肯的回答做了一些修改。我的解决方案如下:

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

public class JEComboBox<T> extends JComboBox<T> {

  public JEComboBox(final T placeHolder){
    setModel(new DefaultComboBoxModel<T>() {
      private static final long serialVersionUID = 1L;
      boolean selectionAllowed = true;

      @Override
      public void setSelectedItem(Object anObject) {
        if (!placeHolder.equals(anObject)) {
          super.setSelectedItem(anObject);
        } else if (selectionAllowed) {
          // Allow this just once
          selectionAllowed = false;
          super.setSelectedItem(anObject);
        }
      }
    });
    addItem(placeHolder);
  }
}

添加占位符时,您将创建一个匿名对象并覆盖toString方法。实施可能如下所示:

public class car{
  String final model;
  public car(String model){
    this.model = model;
  }
}

并创建了JEComboBox:

JEComboBox comboBoxWithPlaceHolder = new JEComboBox<Car>(new Car{
  public String toString(){
    return "- Select your car -"
  }
});

赞成

  • Combobox是通用的

缺点

  • 你需要实现T和Override toString()方法的匿名子类型,因此不会对最终类起作用(如果comboBox保持从接口继承的类,它可能会变得混乱,因为匿名子类型需要实现接口,因此将返回null返回方法。)

答案 2 :(得分:0)

我认为最简单,最快的方法是使用自定义的ListCellRenderer

public class TestComboBox<T> extends JComboBox<T> {

  public TestComboBox() {
    super();
    setRenderer(new ItemRenderer());
  }

  public TestComboBox(ComboBoxModel<T> aModel) {
    super(aModel);
    setRenderer(new ItemRenderer());
  }

  public TestComboBox(T[] items) {
    super(items);
    setRenderer(new ItemRenderer());
  }

  public TestComboBox(Vector<T> items) {
    super(items);
    setRenderer(new ItemRenderer());
  }

  class ItemRenderer extends DefaultListCellRenderer {

    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus){
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

        if (getSelectedItem() == null && index < 0){
            setText("placeholder");
        }
        return this;
    }
  }
}
相关问题