Java:使jcombobox的一项不可选择(如子标题),并编辑该项的字体
如何使组合框中的一个项目不可选择,因为我需要用子主题分隔组合框中的项目 是否可以单独修改特定项目的字体Java:使jcombobox的一项不可选择(如子标题),并编辑该项的字体,java,swing,jcombobox,Java,Swing,Jcombobox,如何使组合框中的一个项目不可选择,因为我需要用子主题分隔组合框中的项目 是否可以单独修改特定项目的字体 jComboBox_btech_course.setFont(new java.awt.Font("Tahoma", 0, 14)); jComboBox_btech_course.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Course" })); j
jComboBox_btech_course.setFont(new java.awt.Font("Tahoma", 0, 14));
jComboBox_btech_course.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Course" }));
jComboBox_btech_course.setName("");
private class theHandler implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
//BTech courses
if(jComboBox_mtech_dept.getSelectedItem().equals("Civil Engineering"))
{
jComboBox_btech_course.removeAllItems();
jComboBox_btech_course.addItem("Building Construction");
jComboBox_btech_course.addItem("Principle And Practice");
jComboBox_btech_course.addItem("Surveying");
jComboBox_btech_course.addItem("Engineering Geology");
jComboBox_btech_course.addItem("Structural Analysis");
jComboBox_btech_course.addItem("Hydraulic Engineering");
jComboBox_btech_course.addItem("Environmental Engineering");
jComboBox_btech_course.addItem("Structural Design");
jComboBox_btech_course.addItem("Geotechnical Engineering");
/*This item has to be unselectable*/
jComboBox_btech_course.addItem("***Sub-topic***");
jComboBox_btech_course.addItem("Transportation Engineering");
jComboBox_btech_course.addItem("Foundation Engineering");
jComboBox_btech_course.addItem("Estimation & Valuation");
jComboBox_btech_course.addItem("Hydrology & Flood Control");
jComboBox_btech_course.addItem("System Analysis, Project Planning And Construction Management");
jComboBox_btech_course.addItem("Irrigation Engineering");
jComboBox_btech_course.addItem("Computer Application in Civil Engineering");
jComboBox_btech_course.addItem("Planning, Design & Detailing");
}
}
}
为了获得所需内容,您需要实现
ComboBoxEditor
通过这种方式,您可以决定在您的情况下要做什么,或者在任何其他情况下您可以通过
addItemListener(ItemListener aListener)
在该方法中,禁用选择或将选择切换到上面的项目左右。前言:在建议的解决方案中,我假设您希望禁用以
“**”开头的项目。您可以将此逻辑更改为您想要的任何逻辑。在改进的版本中,MyComboModel
类(见下文)甚至可以存储禁用的项目,从而允许将任意项目标记为禁用
解决您的问题涉及两件事:
1.不允许选择要禁用的项目
为此,如果要选择的项是禁用项,则可以使用自定义项,并重写其方法以不执行任何操作:
class MyComboModel extends DefaultComboBoxModel<String> {
public MyComboModel() {}
public MyComboModel(Vector<String> items) {
super(items);
}
@Override
public void setSelectedItem(Object item) {
if (item.toString().startsWith("**"))
return;
super.setSelectedItem(item);
};
}
2.使用不同字体显示禁用的项目
为此,您必须使用自定义方法,并且在方法中,您可以为禁用和启用的项目配置不同的视觉外观:
Font f1 = cb.getFont();
Font f2 = new Font("Tahoma", 0, 14);
cb.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (value instanceof JComponent)
return (JComponent) value;
boolean itemEnabled = !value.toString().startsWith("**");
super.getListCellRendererComponent(list, value, index,
isSelected && itemEnabled, cellHasFocus);
// Render item as disabled and with different font:
setEnabled(itemEnabled);
setFont(itemEnabled ? f1 : f2);
return this;
}
});
Font f1=cb.getFont();
字体f2=新字体(“Tahoma”,0,14);
cb.setRenderer(新的DefaultListCellRenderer(){
@凌驾
公共组件GetListCellRenderComponent(JList列表、对象值、,
整型索引,布尔型isSelected,布尔型cellHasFocus){
if(JComponent的值实例)
返回(JComponent)值;
boolean itemEnabled=!value.toString().startsWith(“**”);
super.getListCellRenderComponent(列表、值、索引、,
isSelected&&itemEnabled,cellHasFocus);
//将项目呈现为禁用并使用不同字体:
设置启用(项目启用);
setFont(项目启用?f1:f2);
归还这个;
}
});
Font f1 = cb.getFont();
Font f2 = new Font("Tahoma", 0, 14);
cb.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (value instanceof JComponent)
return (JComponent) value;
boolean itemEnabled = !value.toString().startsWith("**");
super.getListCellRendererComponent(list, value, index,
isSelected && itemEnabled, cellHasFocus);
// Render item as disabled and with different font:
setEnabled(itemEnabled);
setFont(itemEnabled ? f1 : f2);
return this;
}
});