Search code examples
javaswingjcomboboxjung

JComboBox custom renderer is ugly compared to original renderer in GUI


I am working with JUNG and I have designed a custom renderer on the basis of the Show Layouts example... everything is fine in selecting the layouts, but I have to admit that the custom renderer produces a really ugly combo box compared to other jcombobox in my GUI with their original renderer (I use Swing along with NetBeans 7.0). Here's the code for the custom renderer:

class ComboBoxRenderer extends javax.swing.plaf.basic.BasicComboBoxRenderer {

    public ComboBoxRenderer() {
       super();
       setOpaque(true);
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

        if (value instanceof Class) {
            String valueString = value.toString();
            valueString = valueString.substring(valueString.lastIndexOf('.') + 1);
            setText(valueString);
        }
        return this;
    }
}

Is there something I can add to my code in order to maintain the same beautiful style of the original renderers?

Best regards, Simone


Solution

  • You use javax.swing.plaf.basic.BasicComboBoxRenderer which has its own decorations.

    Try to use DefaultListCellRenderer and it will be look pretty, same as your others boxes.

    class ComboBoxRenderer extends DefaultListCellRenderer