Displaying large text in a JComboBox

Problem Statement

I have a list of texts, some of which are large, and I need to display them in a JComboBox. The trouble is, the JComboBox displays all the items in a single line. Consider that the following items need to be displayed:

  1. JNLP is supposed to be the latest and greatest in the Java Applet world post 1.6 release! Personally, I was never a great fan of Applets. I have always felt that Applets are a pain in the wrong place. Inherently difficult and cumbersome to use and deploy. But some of these concerns have been addressed
  2. This is really tiny
  3. As shown above, the 4 progress bars start one after another after the start button is clicked. The following code is for the JPanel with the 4 progress bars: @SuppressWarnings(“serial”) public class ProgressPanel extends JPanel {   private CountDownLatch startTrigger; private CountDownLatch endTrigger;   private JProgressBar bar1; private JSpinner spinner1;   private JProgressBar bar2; private

 

 

I am having the following code that displays a JComboBox in a JFrame:

It looks like this:

Large Text Combo Demo: Default ComboBox Renderer
Large Text Combo Demo: Default ComboBox Renderer

You can see it in action here:  

Note that the JComboBox stretches depending on the width of the largest text as rendered on a single line.

Limiting the width of the JComboBox

We can do that by setting the PrefrerredSize and the MaximumSize of the JComboBox:

It looks like this:

Large Text Combo Demo: Default ComboBox Renderer, With Max and Preferred Size Hints
Large Text Combo Demo: Default ComboBox Renderer, With Max and Preferred Size Hints

You can see it in action here:  

The problem with this approach is that the text is truncated.

Custom Renderer which wraps large test inside HTML

We will have a custom renderer for JComboBox which wraps this text into nice HTML so that it is displayed in multi-lines.

This is how the renderer looks like:

And this is how it looks like now:

Large Text Combo Demo: Custom ComboBox Renderer 1, With Max and Preferred Size Hints
Large Text Combo Demo: Custom ComboBox Renderer 1, With Max and Preferred Size Hints

You can see it in action here:  

As shown above, this does not look good when the pop-up is not there in the JComboBox.

Finishing touches

We need to make a distinction between when there is a pop-out and when there is not. This is done by the int index parameter in the ListCellRenderer::getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) method. When index == -1, it indicates that the item is not popped out. This is the code:

This is how it looks now:

Large Text Combo Demo: Custom ComboBox Renderer 2, With Max and Preferred Size Hints
Large Text Combo Demo: Custom ComboBox Renderer 2, With Max and Preferred Size Hints

You can see it in action here:  

The sources can be found https://github.com/paawak/blog/tree/master/code/Demo.

Leave a Reply

Your email address will not be published. Required fields are marked *