Java Tips Weblog

  • Blog Stats

    • 1,952,036 hits
  • Categories

  • Archives

Combo Box With Separators

Posted by Rob Camick on February 15, 2009

A combo box displays a drop down list of items. Usually all the items in the list are related to one another. Occassionally, you may have a need to show a list of unrelated items. In this case it would be nice to separate the items in the list visually. However, unlike a popup menu, a combo box popup does not support the use of separators.

That is about to change with the introduction of the SeparatorComboBox class. This class will provide the functionality that will allow you to use  a JSeparator as an item in the ComboBoxModel. The functionality provided by the SeparatorComboBox is as follows:

  • a custom renderer is provided to renderer the JSeparator
  • navigation through the popup using the Up/Down keys will cause selection to skip over the separator
  • any attempt to select the separator with the mouse will be ignored and the popup will remain open

Creating a combo box with the following code:

Vector items = new Vector();
items.addElement( "a" );
items.addElement( "b" );
items.addElement( "c" );
items.addElement( new JSeparator() );
items.addElement( "1" );
items.addElement( "2" );
JComboBox comboBox = new SeparatorComboBox( items );

Will result in a combo box looking like this:

combo-box-with-separators

Get The Code

SeparatorComboBox.java

Advertisements

3 Responses to “Combo Box With Separators”

  1. Kleopatra said

    Hi Rob,

    how diiiirty, inserting a view component into a data model :-)

    You are probably aware of some little glitches – the separator can be selected
    – on setting it programatically (concededly not probable in code that uses this trick)
    – visually on rollover, the effect then is that the selection seems to disappear because the separator is so narrow
    – by keyboard navigation if used as a cell editor

    Faintly remember that Santhosh has another trick, involving the renderer only. And – you know what’s coming, me being on a holy quest to spread the word :-) – the SwingX way of life would be to use a Highlighter (not yet possible on a comboBox, only on a JXList)

    Cheers
    Jeanette

    • Rob Camick said

      > the separator can be selected…programatically

      Hmm, I don’t have this problem

      > by keyboard navigation if used as a cell editor

      I’ll have to look into this

      > Faintly remember that Santhosh has another trick

      Found the link to Santhosh’s blog: JComboBox Items with Separators. Its an interesting approach, one I like. I try to find solutions that can be implement without extending a Swing component and this approach seems to do the trick.

      > the SwingX way of life would be to use a Highlighter (not yet possible on a comboBox)

      So I guess there are currently two options available.

      Thanks again for you feedback Jeanette, everybody gains from your knowledge.

    • Rob Camick said

      > by keyboard navigation if used as a cell editor

      Finally got around to looking at this. I can’t think of a way to fix it. So it looks like Santhosh’s solution is the way to go.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: