Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a separator to a JComboBox in Java?

I have a JComboBox and would like to have a separator in the list of elements. How do I do this in Java?

A sample scenario where this would come in handy is when making a combobox for font-family-selection; similar to the font-family-selection-control in Word and Excel. In this case I would like to show the most-used-fonts at the top, then a separator and finally all font-families below the separator in alphabetical order.

Can anyone help me with how to do this or is this not possible in Java?

like image 446
Alfred B. Thordarson Avatar asked Sep 26 '08 11:09

Alfred B. Thordarson


People also ask

How do I add a vertical line to a Java Swing?

Line2D lin = new Line2D. Float(20, 40, 20, 150); First two values are the (x1,y1) value of the starting point of the line and last two values (x2,y2) end point of the line.

What is the difference between JList and JComboBox?

A JComboBox is a component that displays a drop-down list and gives users options that we can select one and only one item at a time whereas a JList shows multiple items (rows) to the user and also gives an option to let the user select multiple items.


1 Answers

There is a pretty short tutorial with an example that shows how to use a custom ListCellRenderer on java2s http://www.java2s.com/Code/Java/Swing-Components/BlockComboBoxExample.htm

Basically it involves inserting a known placeholder in your list model and when you detect the placeholder in the ListCellRenderer you return an instance of 'new JSeparator(JSeparator.HORIZONTAL)'

like image 182
pfranza Avatar answered Sep 21 '22 14:09

pfranza