Search code examples
javaswingjcomboboxlistcellrenderer

Setting initial selected index of JComboBox to -1 or nothing


I recently started learning ListCellRenderer for JComboBox and finally got the basic idea. However, I can't set the initial state or initial selected item of combo box to null (or selected index to -1). I would like to set it to -1 so that when the form loads, nothing is selected yet until user clicks on the drop down to select an item.

I tried using the comboBox.setSelectedIndex(-1) and 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

Like so.

And here's what I keep getting when I launch the form.

enter image description here

GradeLevel combo box is still selected. Index is at 0;

Here's my renderer.

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

I even tried to set the index parameter to -1. index = -1; with no success. Tried list.setSelectedIndex(-1), still won't work.

Any advice or solution?


Solution

  • You're not setting a "default" value for the renderer (or at least you're not checking to see if the value is null).

    Remember, this is shared with ALL the elements in the component, so you MUST configure ALL the properties which might be changed between different object values

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

    This will display --- when the value is not one of the values you are prepared to render