0

I'm customizing my LAF using Synth and now I'm working on the ComboBox which really confuses me. I looked into ComponentProperties Table and found ComboBox.listRenderer property which specifies a renderer for the JComboBox's list. Problem is I don't know how to bind my own renderer to it. I've found some previous answers like:

<style id="ComboBoxListRenderer">
     <opaque value="true"/>
     <state>
          <color type="TEXT_FOREGROUND" value="BLACK" />
     </state>
</style>
<bind style="ComboBoxListRenderer" type="name" key="ComboBox.listRenderer"/>

This does work, but it only changes the default SynthComboBoxRenderer's behavior, and I can't bind my own renderer class ui.MyComboBoxRenderer to it. I've also tried

   <style id="comboBox">
        ...
        <object id="ComboBoxListRenderer" class="ui.MyComboBoxListRenderer"/>
        <property key="ComboBox.listRenderer" type="idref" value="ComboBoxListRenderer"/>
        ...
    </style>
    <bind style="comboBox" type="region" key="ComboBox"/>

Sadly, this time nothing happens at all. Any idea how I can apply a custom renderer to all of the ComboBoxes? Thanks.

H.Wei
  • 103
  • 6

1 Answers1

0

I managed to figure it out myself. For anyone seeking the solution, you need to override SynthComboBoxUI with your own MyComboBoxUI and provide your custom renderer in your UI. For example:

public class MyComboBoxUI extends SynthComboBoxUI {
    //Important! You must define this method and return your own UI class,
    //otherwise Synth will invoke its own *createUI* method and return
    //SynthComboBoxUI instead, which means all your efforts below will be in vain.
    public static ComponentUI createUI(JComponent c) {
        return new MyComboBoxUI();
    }

    //override this method to provide your own list renderer
    @Override
    protected ListCellRenderer<Object> createRenderer() {
        return new MyComboBoxRenderer();
    }

    //custom ListRenderer class
    @SuppressWarnings("serial")
    private class MyComboBoxRenderer extends JLabel implements ListCellRenderer<Object>, UIResource {
        public MyComboBoxRenderer() {
            super();
            setText(" ");
        }

        @Override
        public String getName() {
            // SynthComboBoxRenderer should have installed Name while constructor is working.
            // The setName invocation in the SynthComboBoxRenderer() constructor doesn't work
            // because of the opaque property is installed in the constructor based on the
            // component name (see GTKStyle.isOpaque())
            String name = super.getName();

            return name == null ? "ComboBox.renderer" : name;
        }

        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value,
                         int index, boolean isSelected, boolean cellHasFocus) {
            //implementations
            ...

            // The renderer component should inherit the enabled and
            // orientation state of its parent combobox.  This is
            // especially needed for GTK comboboxes, where the
            // ListCellRenderer's state determines the visual state
            // of the combobox.
            if (comboBox != null){
                setEnabled(comboBox.isEnabled());
                setComponentOrientation(comboBox.getComponentOrientation());
            }

            return this;
        }
    }
}

And the XML file:

<style id="comboBox">
    <insets top="3" left="6" bottom="3" right="6"/>
    <!--fill the full class name in the *value* attribute and synth will do the magic-->
    <defaultsProperty key="ComboBoxUI" type="String" value="ui.MyComboBoxUI"/>
    <state>
        <imagePainter method="comboBoxBackground" path="combobox_normal.png"
            sourceInsets="2 2 2 2"/>
    </state>
</style>
<bind style="comboBox" type="region" key="ComboBox"/>
H.Wei
  • 103
  • 6