Java:使jcombobox中的一个项目不可选(如子标题)并编辑该项目的字体

时间:2015-02-05 12:34:55

标签: 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" }));
        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");
        }
    }
}

3 个答案:

答案 0 :(得分:9)

前言:在建议的解决方案中,我假设您要禁用以"**"开头的项目。您可以将此逻辑更改为您想要的任何内容。在改进版本中,MyComboModel类(见下文)甚至可以存储禁用哪些项目,从而允许将任意项目标记为禁用。

您的问题的解决方案涉及两件事:

1。禁止选择要禁用的项目

为此,您可以使用自定义ComboBoxModel,并覆盖其setSelectedItem()方法,如果要选择的项目是禁用项目,则不执行任何操作:

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);
    };
}

您可以通过将其实例传递给JComboBox构造函数来设置此新模型:

JComboBox<String> cb = new JComboBox<>(new MyComboModel());

2。显示具有不同字体的禁用项目

为此,您必须使用自定义ListCellRenderer,并且在getListCellRendererComponent()方法中,您可以为已禁用和已启用的项目配置不同的可视外观:

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;
    }
});

答案 1 :(得分:1)

为了获得所需,您需要实施ComboBoxEditor

通过这种方式,您可以根据自己的情况或任何其他情况决定要做什么

答案 2 :(得分:1)

您可以通过

添加自定义ItemListener
addItemListener(ItemListener aListener)

并且在该方法中禁用选择或切换选择上面左右的项目。