I have a JComboBox that displays multiple text colors per line. I can't render it using HTML because the combo will contain large values (causing text wrapping). Instead, I created a custom renderer which extends JPanel (then I'm adding different JLabels to that panel which have different foreground/background colors).
In order to display the colors correctly, for each label I have to change setOpaque to true. This overrides the selected item's background color at the top as well:
Is there any way to prevent this behavior? Changing the JPanel's color doesn't change top's background color, but apparently its children JLabels do?
Renderer:
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;
}
}
To set part of the text to red, a simple tag is used:
String comboString = "TEST (<red>Red Text</red>) TEST";