Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox Customer Renderer Problem  RSS feed

 
Mike Davi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm adding a custome renderer to a JComboBox and i'm having a problem with the selected item being highlighted.
What the code below is suppose to do is to paint a red letter "P" next to the item where the first character of the value is a "Y". It seems that the addition of this logic (overriding the Paint method) is keeping the row from highlighting.
Any ideas on how to get both the red "P" to display and the row to highlight?
Thanks.
================================================
private class ProviderLabelRenderer extends JLabel implements ListCellRenderer {
private String valueToRender = "";
private boolean rowIsSelected;
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean hasFocus) {
valueToRender = value.toString();
rowIsSelected = isSelected;
if ((valueToRender == null) || (valueToRender.trim().length() == 2)) {
valueToRender = " ";
}
setText(valueToRender);
return this;
}

public void paint(Graphics g) {
setOpaque(true);
if (valueToRender != "") {
String primaryIndicator = valueToRender.substring(0, 1);
if (primaryIndicator.equals("Y")) {
g.setFont(getFont().deriveFont(Font.BOLD));
g.setColor(Color.red);
g.drawString("P", 1, 12);
} else {
g.setColor(Color.black);
g.drawString(" ", 1, 12);
}
if (rowIsSelected) {
setOpaque(rowIsSelected ? true : false);
setBackground(Color.blue);
} else {
setOpaque(rowIsSelected ? true : false);
setBackground(Color.white);
}
g.drawString(valueToRender.substring(1), 11, 12);
setText(valueToRender);
}
}
}
[ January 23, 2004: Message edited by: Mike Davi ]
[ January 23, 2004: Message edited by: Mike Davi ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!