Dividing a JList

Normally, a list of items in a Java application is presented in a JList or JComboBox. If all of the listed items are created equally, the list handles them quite nicely. However, when you want to organize your list items in groups, there’s no obvious way to do so without using a JTree.

However, inserting dividing lines into your JList or JComboBox (which normally uses a JList as the dropdown component) isn’t so difficult.

Create a ListCellRenderer component for your list. You can do this by implementing the sole required method of the ListCellRenderer interface, or by extending the DefaultListCellRender class. However you do it, the principle is the same. Each time the list items are drawn, the renderer method getListCellRendererComponent( ) is called to obtain a component to be used as a visual template for the list item. To get dividing lines or separators to be drawn, you will need first to ensure that your list model contains items that represent the divider lines. When those items are encountered, simply return a component that looks like a dividing line. The following sample method shows the technique applied to list items that have an underscore as their toString( ) return value.

 public Component getListCellRendererComponent(JList list, Object value, intindex, boolean isSelected, boolean cellHasFocus) {	if ( value != null && value.toString( ).equals("_") )	{		JLabel lblSeparator = new JLabel( );		lblSeparator.setBorder( BorderFactory.createLineBorder(Color.black ) );		lblSeparator.setPreferredSize( new Dimension( 2, 2 ) );		return lblSeparator ;	}	else		return super.getListCellRendererComponent( list, value,index, isSelected, cellHasFocus);}

The key here is to use setPreferredSize( ) to fix the row height at 2 pixels. This size is only used when the list is initially laid out. After that, the row height can’t be changed as easily. The label border draws the black line separator. Set the list cell renderer using list.setCellRenderer() for JLists and combo.setRenderer( ) for JComboBoxes.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may