Browse Source

Apply combobox render background if set.

Respect custom font of renderer.
pull/222/head
weisj 4 years ago
parent
commit
7f6681e33f
  1. 30
      core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxUI.java
  2. 8
      utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java

30
core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxUI.java

@ -213,13 +213,16 @@ public class DarkComboBoxUI extends BasicComboBoxUI implements ComboBoxConstants
@Override
public void paint(final Graphics g, final JComponent c) {
paintBackground(g, c);
if (!comboBox.isEditable()) {
paintCurrentValue(g, rectangleForCurrentValue(), hasFocus);
Component currentValueRenderer = getRendererForCurrentValue();
paintBackground(g, c, currentValueRenderer);
paintCurrentValue(g, rectangleForCurrentValue(), hasFocus, currentValueRenderer);
} else {
paintBackground(g, c, getEditorComponent());
}
}
private void paintBackground(final Graphics g, final JComponent c) {
private void paintBackground(final Graphics g, final JComponent c, final Component currentValueRenderer) {
final Container parent = c.getParent();
if (parent != null && parent.isOpaque() && !c.isEnabled()) {
g.setColor(parent.getBackground());
@ -227,7 +230,9 @@ public class DarkComboBoxUI extends BasicComboBoxUI implements ComboBoxConstants
return;
}
boolean isCellEditor = ComboBoxConstants.isTreeOrTableCellEditor(c);
Color bg = getBackground(comboBox);
Color rendererBg = currentValueRenderer != null ? currentValueRenderer.getBackground() : null;
Color bg = PropertyUtil.chooseColor(rendererBg, getBackground(comboBox));
Color splitBg = getArrowBackground(comboBox);
Rectangle arrowRect = comboBox.isEditable() ? arrowButton.getBounds() : null;
DividedWidgetPainter.paintBackground((Graphics2D) g, c, arcSize, arrowRect, bg, splitBg, isCellEditor);
@ -326,17 +331,20 @@ public class DarkComboBoxUI extends BasicComboBoxUI implements ComboBoxConstants
}
@SuppressWarnings("unchecked")
public void paintCurrentValue(final Graphics g, final Rectangle bounds, final boolean hasFocus) {
ListCellRenderer<Object> renderer = comboBox.getRenderer();
Component c = renderer.getListCellRendererComponent(listBox, comboBox.getSelectedItem(),
protected Component getRendererForCurrentValue() {
return comboBox.getRenderer().getListCellRendererComponent(listBox, comboBox.getSelectedItem(),
comboBox.getSelectedIndex(), false, false);
}
public void paintCurrentValue(final Graphics g, final Rectangle bounds, final boolean hasFocus, final Component c) {
PropertyUtil.installFont(c, comboBox.getFont());
c.setFont(comboBox.getFont());
if (hasFocus && !isPopupVisible(comboBox)) {
c.setForeground(listBox.getForeground());
c.setBackground(listBox.getBackground());
PropertyUtil.installForeground(c, listBox.getForeground());
PropertyUtil.installBackground(c, listBox.getBackground());
} else {
c.setForeground(getForeground(comboBox));
c.setBackground(getBackground(comboBox));
PropertyUtil.installForeground(c, getForeground(comboBox));
PropertyUtil.installBackground(c, getBackground(comboBox));
}
if (c instanceof JComponent) {
PropertyUtil.installBorder((JComponent) c, null);

8
utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java

@ -59,6 +59,14 @@ public class PropertyUtil {
}
}
public static void installFont(final Component component, final Font font) {
if (component == null) return;
Font f = component.getFont();
if (f == null || f instanceof UIResource) {
component.setFont(font);
}
}
public static void installProperty(final JComponent c, final String key, final Object value) {
if (c.getClientProperty(key) == null) {
c.putClientProperty(key, value);

Loading…
Cancel
Save