Java JComboBox自定义渲染器和GTK

时间:2009-06-25 18:30:02

标签: java linux swing gtk jcombobox

我有一个Customer对象列表,我需要从JComboBox中选择它们。根据我的阅读,我需要实现一个自定义渲染器,让我想要的字段显示在列表中。

我希望我的JComboBox具有如下格式的条目:

+----------------------------------------------+
|  Customer Name - Contact - City, State    V  |
+==============================================+
|  Customer #2 Name - Contact - City, State    |
|  Customer #3 Name - Contact - City, State    |
|  Customer #4 Name - Contact - City, State    |
|  Customer #5 Name - Contact - City, State    |
+----------------------------------------------+

我使用了这段代码:

公共类CustomerListCellRenderer扩展了DefaultListCellRenderer {

@Override
public Component getListCellRendererComponent(
        JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    if (value instanceof Customer) {
        Customer c = (Customer) value;

        StringBuffer sb = new StringBuffer();
        if (c.getCompany() != null && c.getCompany().length() > 0) {
            sb.append(c.getCompany());
        }
        sb.append(" - ");
        if (c.getCompany() != null && c.getCompany().length() > 0) {
            sb.append(c.getContact());
        }
        sb.append(" - ");
        if (c.getCompany() != null && c.getCompany().length() > 0) {
            sb.append(c.getCity());
            sb.append(", ");
        }            
        if (c.getCompany() != null && c.getCompany().length() > 0) {
            sb.append(c.getState());
        }

        setText(sb.toString());
    }
    return this;
  }
}

使用系统GTKLookAndFeel在Solaris / Unix / Linux下无法正常工作。未绘制组合框输入区域的背景,并且不会在其周围绘制边框。 (见下面的截图)。还有另一种方法可以在3个主要平台(Win / Mac / GTK)上正常工作吗?我可以使用转换器来执行此操作并仅操作数据而非GUI吗?

我目前的解决方法是覆盖我的Customer对象上的toString(),以我想要的格式显示每条记录,但寻找其他想法。

alt text

尼克

3 个答案:

答案 0 :(得分:2)

同样的问题,我这样做是为了自定义它以显示图标:

private static class CustomComboBoxRenderer extends DefaultListCellRenderer
{
    private final ListCellRenderer backend;

    public CustomComboBoxRenderer(ListCellRenderer backend)
    {
        this.backend = backend;
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
        Component component = backend.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if(component instanceof JLabel == false)
            component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        JLabel label = (JLabel)component;
        label.setIcon(Icons.COMPONENT);
        return label;
    }
}

然后像这样分配渲染器:

comboBox.setRenderer(new CustomComboBoxRenderer(comboBox.getRenderer()));

到目前为止,这对我来说还不错。

答案 1 :(得分:1)

试试这个:

public Component getListCellRendererComponent(
        JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    if (value instanceof Customer) {
        Customer c = (Customer) value;

        StringBuffer sb = new StringBuffer();
        if (c.getCompany() != null && c.getCompany().length() > 0) {
            sb.append(c.getCompany());
        }
        sb.append(" - ");
        if (c.getCompany() != null && c.getCompany().length() > 0) {
            sb.append(c.getContact());
        }
        sb.append(" - ");
        if (c.getCompany() != null && c.getCompany().length() > 0) {
            sb.append(c.getCity());
            sb.append(", ");
        }            
        if (c.getCompany() != null && c.getCompany().length() > 0) {
            sb.append(c.getState());
        }

        value = sb.toString();
    } 
    return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
  }
}

还使用StringBuilder而不是StringBuffer(这是单线程情况)。

此外,您还可以在代码中剪切和粘贴错误,例如:

        if (c.getCompany() != null && c.getCompany().length() > 0) {
            sb.append(c.getState());
        }

正在检查公司成员并使用州成员。

答案 2 :(得分:0)

DefaultListCellRenderer扩展了JLabel,看起来像JLabel。如果您有不可编辑的ComboBox,则通过getRenderer返回的Renderer用于绘制下拉列表区域以及“输入”区域。尝试使用ComboBox和渲染器的边框/前景/背景设置。