【问题标题】:Binding a custom renderer to a ComboBox using Synth使用 Synth 将自定义渲染器绑定到 ComboBox
【发布时间】:2018-07-16 00:56:27
【问题描述】:

我正在使用 Synth 自定义我的 LAF,现在我正在研究 ComboBox,这让我很困惑。我查看了ComponentProperties Table 并找到了ComboBox.listRenderer 属性,它为JComboBox 的列表指定了一个渲染器。问题是我不知道如何将我自己的渲染器绑定到它。我找到了一些以前的答案,例如:

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

这确实有效,但它只会改变默认的SynthComboBoxRenderer 的行为,我无法将我自己的渲染器类ui.MyComboBoxRenderer 绑定到它。我也试过了

   <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"/>

很遗憾,这一次什么都没有发生。知道如何将自定义渲染器应用于所有组合框吗?谢谢。

【问题讨论】:

    标签: java swing combobox synth


    【解决方案1】:

    我自己设法弄明白了。对于任何寻求解决方案的人,您需要用您自己的MyComboBoxUI 覆盖SynthComboBoxUI,并在您的 UI 中提供您的自定义渲染器。例如:

    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;
            }
        }
    }
    

    还有 XML 文件:

    <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"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-08
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      相关资源
      最近更新 更多