Java:使一个jcombobox项目无法选择(如子标题)并编辑该项目的字体 [英] Java: Make one item of a jcombobox unselectable(like for a sub-caption) and edit font of that item

查看:171
本文介绍了Java:使一个jcombobox项目无法选择(如子标题)并编辑该项目的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在组合框不可选择中制作一个项目,因为我需要将组合框中的项目与子主题分开

How to make one item in a combobox unselectable because I need to separate items in a combobox with a sub-topic.

是否可以单独修改该特定项目的字体?

And is it possible to modify the font of that particular item individually?

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


推荐答案

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

Foreword: In the proposed solution I assume that you want to disable items that start with "**". You can change this logic to whatever you want to. In an improved version the MyComboModel class (see below) may even store which items are disabled allowing arbitrary items to be marked disabled.

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

Solution to your question involves 2 things:

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

For this you can use a custom ComboBoxModel, and override its setSelectedItem() method to do nothing if the item to be selected is a disabled one:

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

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

And you can set this new model by passing an instance of it to the JComboBox constructor:

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



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



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

2. Display disabled items with different font

For this you have to use a custom ListCellRenderer and in getListCellRendererComponent() method you can configure different visual appearance for disabled and enabled items:

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

这篇关于Java:使一个jcombobox项目无法选择(如子标题)并编辑该项目的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆