Hi,

If I have a JComboBox which has a list of Strings which can be selected, is there a way to insert a line/horizontal rule inside to indicate some sort of grouping of values? If so, how do I do it? (Also, the actual line ------ should not be selectable in the dropdown. It is merely there to differentiate groups of values..)

Thanks,
winbatch

Recommended Answers

All 5 Replies

Sounds like you may be better off implementing a JTextField in combination with a PopupMenu. There you can use the menu separator. Pack them together into a custom component and you will be able to use it just like a JComboBox.

Sounds like you may be better off implementing a JTextField in combination with a PopupMenu. There you can use the menu separator. Pack them together into a custom component and you will be able to use it just like a JComboBox.

Would you happen to have some sample code?

Not here, and not now. Actually for this exact type of thing, not at all any more. If I still had access to the code from certain parts of the last project I worked on I would have something very similar, but I don't.

Here is the design idea.

Start with a JPanel implementing ActionLisitener
Add an ArrayList (or something else to act as the DataModel)
Add a JTextField
Add a button to the Left that opens a popupmenu
The Action Listener takes the selected menu item, adds it to the JTextfield and fires an ItemEvent

There are a few tweaks still to make, but that is the gist.

Have Fun.

Sounds like you may be better off implementing a JTextField in combination with a PopupMenu.

All this can be done without that much hassle. The person above just needs to implement a ListCellRenderer object and a ComboBoxModel which would do the task for him/her.

First, this is two years old, I doubt he's waiting any more. Second, don't forget that then the separator is also selectable and that he will have to add some hooks to either prevent it's selection actually doing anything, or better yet that it can't be selected at all, and then you're right back to about the same amount of work as the other possibilty.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.