|
We
got this functionality by stuffing a JSeparator into the combo model where the
separator should appear and then overriding BasicComboBoxRenderer to return a
specialized renderer.
Here
is the implementation of our BasicComboBoxRenderer:
/**
* Override super.getListCellRendererComponent() */ public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { // ...
Component renderer =
super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus); // other logic
here....
// create a specialized renderer is the
model value is a JSeparator....
if (value instanceof JSeparator)
{
renderer = getSeparator(); } return renderer;
} /**
* Creates a list separator. */ private JPanel getSeparator() { if (_sepPanel != null) return _sepPanel; // we put the separator inside a panel to
control
// its appearance _sepPanel = new JPanel(); Border sepBorder = BorderFactory.createEmptyBorder(1, 3, 1, 3); _sepPanel.setOpaque(false); _sepPanel.setBorder(sepBorder); _sepPanel.setLayout(new BoxLayout(_sepPanel, BoxLayout.Y_AXIS)); _sepPanel.add(Box.createRigidArea(new Dimension(0, 4))); _sepPanel.add(new JPopupMenu.Separator()); _sepPanel.add(Box.createRigidArea(new Dimension(0, 4))); return _sepPanel; } You also want to create a subclass of JComboBox, overriding
setSelectedIndex and setSelectedItem so that these methods skip selection of the
JSeparator in the combo list.
|
- Separator in Combo box Lim Ting
- Farwell, Paul
