将 JComboBox 的初始选定索引设置为 -1 或什么都不设置
Setting initial selected index of JComboBox to -1 or nothing
最近为了JComboBox
开始学习ListCellRenderer
,终于掌握了基本思路。但是,我无法将组合框的初始状态或初始 selected 项设置为 null
(或 selected 索引为 -1)。我想将它设置为 -1,以便在加载表单时,在用户单击下拉列表到 select 一个项目之前,不会 select 编辑任何内容。
我尝试使用 comboBox.setSelectedIndex(-1)
和 comboBox.setSelectedItem(null)
GradeLevelDaoImpl gldi = new GradeLevelDaoImpl();
DefaultComboBoxModel gradeLevelModel = new DefaultComboBoxModel(gldi.getAllActiveGradeLevels().toArray());
jcmbGradeLevel.setModel(gradeLevelModel);
jcmbGradeLevel.setRenderer(new JComboBoxRenderer());
jcmbGradeLevel.setSelectedItem(null); //doesn't work
jcmbGradeLevel.setSelectedIndex(-1); //doesn't work
像这样。
这是我启动表单时不断收到的信息。
GradeLevel
组合框仍然是 selected。索引为 0;
这是我的渲染器。
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
//Class value conversion to getString value using getter
if (value instanceof SchoolYear) {
this.setText("" + ((SchoolYear) value).getStart());
}
if (value instanceof GradeLevel) {
this.setText("" + ((GradeLevel) value).getGradelevel());
}
if (value instanceof PaymentTerm) {
this.setText("" + ((PaymentTerm) value).getPaymentTerm());
}
if (value instanceof FeeCategory) {
this.setText("" + ((FeeCategory) value).getFeeCategory());
}
//selection formatting
if (isSelected) {
this.setBackground(Color.YELLOW);
//this.setBackground(list.getSelectionBackground());
this.setForeground(list.getSelectionForeground());
} else {
this.setBackground(list.getBackground());
this.setForeground(list.getForeground());
}
if ((isSelected) && (cellHasFocus)) {
this.setBorder(new LineBorder(Color.black));
} else {
this.setBorder(null);
}
return this;
}
我什至尝试将 index
参数设置为 -1。 index = -1;
没有成功。
试了list.setSelectedIndex(-1)
,还是不行。
有什么建议或解决方案吗?
您没有为渲染器设置 "default" 值(或者至少您没有检查 value
是否为 null
)。
请记住,这与组件中的所有元素共享,因此您必须配置所有可能在不同对象值之间更改的属性
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
//Class value conversion to getString value using getter
if (value instanceof SchoolYear) {
this.setText("" + ((SchoolYear) value).getStart());
} else if (value instanceof GradeLevel) {
this.setText("" + ((GradeLevel) value).getGradelevel());
} else if (value instanceof PaymentTerm) {
this.setText("" + ((PaymentTerm) value).getPaymentTerm());
} else if (value instanceof FeeCategory) {
this.setText("" + ((FeeCategory) value).getFeeCategory());
} else {
this.setText("---");
}
//selection formatting
if (isSelected) {
this.setBackground(Color.YELLOW);
//this.setBackground(list.getSelectionBackground());
this.setForeground(list.getSelectionForeground());
} else {
this.setBackground(list.getBackground());
this.setForeground(list.getForeground());
}
if ((isSelected) && (cellHasFocus)) {
this.setBorder(new LineBorder(Color.black));
} else {
this.setBorder(null);
}
return this;
}
当值不是您准备呈现的值之一时,这将显示 ---
最近为了JComboBox
开始学习ListCellRenderer
,终于掌握了基本思路。但是,我无法将组合框的初始状态或初始 selected 项设置为 null
(或 selected 索引为 -1)。我想将它设置为 -1,以便在加载表单时,在用户单击下拉列表到 select 一个项目之前,不会 select 编辑任何内容。
我尝试使用 comboBox.setSelectedIndex(-1)
和 comboBox.setSelectedItem(null)
GradeLevelDaoImpl gldi = new GradeLevelDaoImpl();
DefaultComboBoxModel gradeLevelModel = new DefaultComboBoxModel(gldi.getAllActiveGradeLevels().toArray());
jcmbGradeLevel.setModel(gradeLevelModel);
jcmbGradeLevel.setRenderer(new JComboBoxRenderer());
jcmbGradeLevel.setSelectedItem(null); //doesn't work
jcmbGradeLevel.setSelectedIndex(-1); //doesn't work
像这样。
这是我启动表单时不断收到的信息。
GradeLevel
组合框仍然是 selected。索引为 0;
这是我的渲染器。
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
//Class value conversion to getString value using getter
if (value instanceof SchoolYear) {
this.setText("" + ((SchoolYear) value).getStart());
}
if (value instanceof GradeLevel) {
this.setText("" + ((GradeLevel) value).getGradelevel());
}
if (value instanceof PaymentTerm) {
this.setText("" + ((PaymentTerm) value).getPaymentTerm());
}
if (value instanceof FeeCategory) {
this.setText("" + ((FeeCategory) value).getFeeCategory());
}
//selection formatting
if (isSelected) {
this.setBackground(Color.YELLOW);
//this.setBackground(list.getSelectionBackground());
this.setForeground(list.getSelectionForeground());
} else {
this.setBackground(list.getBackground());
this.setForeground(list.getForeground());
}
if ((isSelected) && (cellHasFocus)) {
this.setBorder(new LineBorder(Color.black));
} else {
this.setBorder(null);
}
return this;
}
我什至尝试将 index
参数设置为 -1。 index = -1;
没有成功。
试了list.setSelectedIndex(-1)
,还是不行。
有什么建议或解决方案吗?
您没有为渲染器设置 "default" 值(或者至少您没有检查 value
是否为 null
)。
请记住,这与组件中的所有元素共享,因此您必须配置所有可能在不同对象值之间更改的属性
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
//Class value conversion to getString value using getter
if (value instanceof SchoolYear) {
this.setText("" + ((SchoolYear) value).getStart());
} else if (value instanceof GradeLevel) {
this.setText("" + ((GradeLevel) value).getGradelevel());
} else if (value instanceof PaymentTerm) {
this.setText("" + ((PaymentTerm) value).getPaymentTerm());
} else if (value instanceof FeeCategory) {
this.setText("" + ((FeeCategory) value).getFeeCategory());
} else {
this.setText("---");
}
//selection formatting
if (isSelected) {
this.setBackground(Color.YELLOW);
//this.setBackground(list.getSelectionBackground());
this.setForeground(list.getSelectionForeground());
} else {
this.setBackground(list.getBackground());
this.setForeground(list.getForeground());
}
if ((isSelected) && (cellHasFocus)) {
this.setBorder(new LineBorder(Color.black));
} else {
this.setBorder(null);
}
return this;
}
当值不是您准备呈现的值之一时,这将显示 ---