我设法自己弄清楚了。对于任何寻求解决方案的人,您需要SynthComboBoxUI
使用您自己的覆盖MyComboBoxUI
并在您的 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"/>