0

I'm working with a JList which has a custom cell renderer. This is the cell renderer :

public class DefaultOriginalPublisherListRenderer extends DefaultListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        if (value instanceof OriginalPublisher) {
            OriginalPublisher op = (OriginalPublisher) value;
            String text = op.getName();
            if (index > -1) {
                text = "<html><b>" + op.getName() + "</b><br/><small>" + op.getIpi() + "</small></html>";
            }
            return super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
        } else {
            return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        }

    }
}

All it does is that it makes a few changes to the text when the list is expanded. Otherwise, it does nothing.

It should be added that the application runs with the default L&F for the given system / OS where the application runs. On my system the default cell renderer is class javax.swing.plaf.synth.SynthComboBoxUI$SynthComboBoxRenderer. That's the cell renderer I'm replacing with the above renderer.

The JList which the renderer is applied to looks like this. You can see that the height is much less than the component above :

enter image description here

What I'd like the height to be is this. That's the "default" height when no cell renderer is used :

enter image description here

How can I ensure that the height of the JList, even with a custom cell renderer applied, remains the default?

Here's a SSCCE :

package test;

import java.awt.Component;
import java.awt.Dimension;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class CellRendererTest {

    private final JFrame f;

    public CellRendererTest() {
        f = new JFrame();
        init(f);
    }

    public void show() {
        f.setPreferredSize(new Dimension(640, 480));
        f.pack();
        f.setVisible(true);
    }

    private void init(JFrame f) {
        JPanel p = new JPanel();
        f.add(p);

        JComboBox defaultRendererCb = new JComboBox();
        p.add(defaultRendererCb);

        JComboBox customRendererCb = new JComboBox();
        customRendererCb.setRenderer(new DefaultOriginalPublisherListRenderer());
        p.add(customRendererCb);        

        DefaultComboBoxModel m = new DefaultComboBoxModel();
        m.addElement(new OriginalPublisher("Test 1", "12345"));
        m.addElement(new OriginalPublisher("Test 1", "67890"));
        defaultRendererCb.setModel(m);
        customRendererCb.setModel(m);
    }

    private class DefaultOriginalPublisherListRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            if (value instanceof OriginalPublisher) {
                OriginalPublisher op = (OriginalPublisher) value;
                String text = op.getName();
                if (index > -1) {
                    text = "<html><b>" + op.getName() + "</b><br/><small>" + op.getNumber() + "</small></html>";
                }
                return super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
            } else {
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            }
        }
    }

    private class OriginalPublisher {

        private final String name;
        private final String number;

        public OriginalPublisher(String name, String number) {
            this.name = name;
            this.number = number;
        }

        public String getName() {
            return name;
        }

        public String getNumber() {
            return number;
        }
    }

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        CellRendererTest t = new CellRendererTest();
        t.show();
    }
}
sbrattla
  • 5,274
  • 3
  • 39
  • 63
  • 1
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) *"Why is the height of a JList changed when using a custom cell renderer?"* Based on the little code I see, I'd guess it is based on HTML formatting, and/or using `
    ` in the HTML.
    – Andrew Thompson Apr 21 '20 at 08:42
  • Have tried your [mcve] but got the same height of comboboxes. – Sergiy Medvynskyy Apr 21 '20 at 09:39
  • HTML might use a different font and font size? – camickr Apr 21 '20 at 13:52
  • @SergiyMedvynskyy what OS are you on? It may be an issue related to the L&F on Ubuntu I'm using. – sbrattla Apr 21 '20 at 13:58
  • @sbrattla I use Windows. Sorry but cannot test it on Ubuntu. Try to use another L&F for example Nimbus. It's not OS-depended. – Sergiy Medvynskyy Apr 21 '20 at 14:35

0 Answers0