Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Make one item of a jcombobox unselectable(like for a sub-caption) and edit font of that item

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");
        }
    }
}
like image 576
Smiley Avatar asked Feb 05 '15 12:02

Smiley


People also ask

Which event gets generated when you select an item from a JComboBox?

The combo box fires an action event when the user selects an item from the combo box's menu.

What is the difference between JComboBox and JList box give one method of each of them?

A JComboBox is a component that displays a drop-down list and gives users options that we can select one and only one item at a time whereas a JList shows multiple items (rows) to the user and also gives an option to let the user select multiple items.

Which of these is used to determine whether the JComboBox is editable?

SetEditable() - Determines whether the JComboBox field is editable.


2 Answers

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:

1. Disallow selecting items which you want to be disabled

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

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. 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;
    }
});
like image 122
icza Avatar answered Oct 05 '22 23:10

icza


In order to get what you need, you need to implement ComboBoxEditor.

In this way, you can decide what you want to do in your case, or in any other cases

like image 29
roeygol Avatar answered Oct 05 '22 22:10

roeygol