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

Loading…
Cancel
Save