JCombobox列表背景颜色覆盖选定的背景项颜色

时间:2016-01-04 23:20:47

标签: java swing jcombobox listcellrenderer

我有一个JComboBox,每行显示多种文本颜色。我无法使用HTML呈现它,因为组合将包含大值(导致文本换行)。相反,我创建了一个自定义渲染器,它扩展了JPanel(然后我将不同的JLabel添加到具有不同前景/背景颜色的面板)。

为了正确显示颜色,对于每个标签,我必须将setOpaque更改为true。这也会覆盖顶部选定项目的背景颜色:

enter image description here

有没有办法阻止这种行为?更改JPanel的颜色不会改变顶部的背景颜色,但显然它的孩子JLabel会这样做吗?

渲染器:

import java.awt.Color;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JList;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

public class CustomComboBoxRenderer extends JPanel implements ListCellRenderer {

    private JLabel[] labels = { new JLabel(), new JLabel(), new JLabel() };

    public CustomComboBoxRenderer() {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

        for (JLabel label : labels) {
            label.setOpaque(true);
            label.setVerticalAlignment(JLabel.CENTER);
            add(label);
        }
    }

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

        if(value != null) {
            String valueString = value.toString();

            if(valueString.contains("<red>")) {
                int redStart = valueString.indexOf("<red>");
                valueString = valueString.replace("<red>", "");
                int redEnd = valueString.indexOf("</red>");
                valueString = valueString.replace("</red>", "");

                if(redStart > 0) {
                    labels[0].setText(valueString.substring(0, redStart));
                }

                labels[1].setText(valueString.substring(redStart, redEnd));

                if(redEnd < valueString.length()) {
                    labels[2].setText(valueString.substring(redEnd, valueString.length()));
                }
            } else {
                labels[0].setText(valueString);
                labels[1].setText("");
                labels[2].setText("");
            }
        }

        if (isSelected) {
            labels[0].setBackground(list.getSelectionBackground());
            labels[1].setBackground(list.getSelectionBackground());
            labels[2].setBackground(list.getSelectionBackground());

            labels[0].setForeground(list.getSelectionForeground());
            labels[1].setForeground(Color.RED);
            labels[2].setForeground(list.getSelectionForeground());
        } else {
            labels[0].setBackground(list.getBackground());
            labels[1].setBackground(list.getBackground());
            labels[2].setBackground(list.getBackground());

            labels[0].setForeground(list.getForeground());
            labels[1].setForeground(Color.RED);
            labels[2].setForeground(list.getForeground());
        }

        setBackground(list.getBackground());
        return this;
    }
}

要将部分文本设置为红色,请使用简单标记:

String comboString = "TEST (<red>Red Text</red>) TEST";

1 个答案:

答案 0 :(得分:3)

通常在这种情况下,我只在选择项目时才设置不透明状态

setOpaque(isSelected);