我有一个JComboBox
,包含三个项目{"Personel", "Magasinier", "Fournisseur"}
。
我想让这个JComboBox
显示值"Choisir une option :"
,这是一个不可选择的值。
我在initComponents();
之后尝试了这个代码:
this.jComboBox1.setSelectedItem("Choisir une option :");
但是它不工作。
您可以覆盖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 -
"的组合框。一旦用户选择了另一个选项,就不可能再选择原来的选项。
我偶然发现了这个问题,并对邓肯的回答做了一些修改。我的解决方案是这样的:
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()方法的匿名子类型,因此不能在final类上工作(如果comboBox持有从接口继承的类,它会变得混乱,因为匿名子类型需要实现接口,因此将有null返回方法)
我认为最简单和最快的方法是使用自定义的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;
}
}
}