From 7f6681e33f42d75516efac30657417108a2a871d Mon Sep 17 00:00:00 2001 From: weisj Date: Tue, 3 Nov 2020 17:26:03 +0100 Subject: [PATCH] Apply combobox render background if set. Respect custom font of renderer. --- .../darklaf/ui/combobox/DarkComboBoxUI.java | 30 ++++++++++++------- .../weisj/darklaf/util/PropertyUtil.java | 8 +++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxUI.java index 5ad71863..0d963223 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/combobox/DarkComboBoxUI.java +++ b/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 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); diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java b/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java index 069c1745..bd2bc70f 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java +++ b/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);