Search code examples
javaswingcomboboxoptgroup

What is the Swing-equivalent to HTML <optgroup>


I want my JComboBox to group multiple options together, similar to the HTML optgroup:

<select>  
 <optgroup label="A">  
  <option/>
  <option/>  
 </optgroup>
</select>  

I could not find any solution for this in Swing. Manipulating the UI-Renderer for the Combobox seems to be a bad idea, as it's OS & L&F-dependent (and they are private so cannot extend).


Solution

  • Consider the following implementation as a basic guide how to apply custom styling and create non-selectable items:

    public class ExtendedComboBox extends JComboBox {
    
        public ExtendedComboBox() {
            setModel(new ExtendedComboBoxModel());
            setRenderer(new ExtendedListCellRenderer());
        }
    
        public void addDelimiter(String text) {
            this.addItem(new Delimiter(text));
        }
    
        private static class ExtendedComboBoxModel extends DefaultComboBoxModel {
            @Override
            public void setSelectedItem(Object anObject) {
                if (!(anObject instanceof Delimiter)) {
                    super.setSelectedItem(anObject);
                } else {
                    int index = getIndexOf(anObject);
                    if (index < getSize()) {
                        setSelectedItem(getElementAt(index+1));
                    }
                }
            }
    
        }
    
        private static class ExtendedListCellRenderer 
                        extends DefaultListCellRenderer {
    
            @Override
            public Component getListCellRendererComponent(JList list, Object value,
                            int index, boolean isSelected, boolean cellHasFocus) {
                if (!(value instanceof Delimiter)) {
                    return super.getListCellRendererComponent(list, value, index,
                            isSelected, cellHasFocus);
                } else {
                    JLabel label = new JLabel(value.toString());
                    Font f = label.getFont();
                    label.setFont(f.deriveFont(f.getStyle() 
                               | Font.BOLD | Font.ITALIC));
                    return label;
                }
            }
        }
    
        private static class Delimiter {
            private String text;
    
            private Delimiter(String text) {
                this.text = text;
            }
    
            @Override
            public String toString() {
                return text.toString();
            }
        }
    }