diff --git a/src/main/java/com/github/weisj/darklaf/ui/cell/DarkCellRendererToggleButton.java b/src/main/java/com/github/weisj/darklaf/ui/cell/DarkCellRendererToggleButton.java index 59174585..44267e03 100644 --- a/src/main/java/com/github/weisj/darklaf/ui/cell/DarkCellRendererToggleButton.java +++ b/src/main/java/com/github/weisj/darklaf/ui/cell/DarkCellRendererToggleButton.java @@ -114,6 +114,8 @@ public class DarkCellRendererToggleButton) editorComponent).removeAllItems(); ((JComboBox) editorComponent).addItem(value); ((JComboBox) editorComponent).setSelectedItem(value); - } else if (editorComponent instanceof JToggleButton && adjustBoolValue) { + } else if (editorComponent instanceof JToggleButton) { ((JToggleButton) editorComponent).setSelected(!(((JToggleButton) editorComponent).isSelected())); SwingUtilities.invokeLater(tree::stopEditing); } editorComponent.setOpaque(false); editorComponent.setComponentOrientation(tree.getComponentOrientation()); - - if (isSelected && editorComponent instanceof JToggleButton) { - if (DarkUIUtil.hasFocus(tree)) { + if (editorComponent instanceof JToggleButton) { + if (DarkUIUtil.hasFocus(tree) || DarkUIUtil.hasFocus(editorComponent)) { editorComponent.setForeground(UIManager.getColor("Tree.selectionForeground")); } else { editorComponent.setForeground(UIManager.getColor("Tree.selectionForegroundInactive")); diff --git a/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeCellRenderer.java b/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeCellRenderer.java index 3110b76b..c716a747 100644 --- a/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeCellRenderer.java +++ b/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeCellRenderer.java @@ -59,7 +59,7 @@ public class DarkTreeCellRenderer extends DefaultTreeCellRenderer implements Tre return rendererComponent; } Component comp = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); - if (sel && !DarkUIUtil.hasFocus(tree)) { + if (sel && !(DarkUIUtil.hasFocus(tree) || DarkUIUtil.hasFocus(comp))) { comp.setForeground(UIManager.getColor("Tree.selectionForegroundInactive")); } return comp; diff --git a/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeUI.java b/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeUI.java index b3a0d522..2fa67a87 100644 --- a/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeUI.java +++ b/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeUI.java @@ -23,6 +23,7 @@ */ package com.github.weisj.darklaf.ui.tree; +import com.github.weisj.darklaf.util.DarkUIUtil; import com.github.weisj.darklaf.util.SystemInfo; import javax.swing.*; @@ -53,7 +54,7 @@ public class DarkTreeUI extends BasicTreeUI implements PropertyChangeListener { public static final String TREE_TABLE_TREE_KEY = "JTree.treeTableTree"; public static final String STRIPED_CLIENT_PROPERTY = "JTree.alternateRowColor"; - private final MouseListener mySelectionListener = new MouseAdapter() { + private final MouseListener selectionListener = new MouseAdapter() { boolean handled = false; @Override @@ -167,7 +168,7 @@ public class DarkTreeUI extends BasicTreeUI implements PropertyChangeListener { protected void installListeners() { super.installListeners(); tree.addPropertyChangeListener(this); - tree.addMouseListener(mySelectionListener); + tree.addMouseListener(selectionListener); } @Override @@ -298,18 +299,40 @@ public class DarkTreeUI extends BasicTreeUI implements PropertyChangeListener { @Override protected FocusListener createFocusListener() { return new FocusListener() { + + boolean focused = false; + @Override public void focusGained(final FocusEvent e) { - tree.repaint(); + if (!focused) { + tree.repaint(); + } + focused = true; } @Override public void focusLost(final FocusEvent e) { - tree.repaint(); + tree.stopEditing(); + if (!hasFocus()) { + tree.repaint(); + focused = false; + } } }; } + protected boolean hasFocus() { + if (!DarkUIUtil.hasFocus(tree)) { + Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); + boolean treeEditor = owner instanceof JComponent + && Boolean.TRUE.equals(((JComponent) owner).getClientProperty("JToggleButton.isTreeCellEditor")); + boolean treeRenderer = owner instanceof JComponent + && Boolean.TRUE.equals(((JComponent) owner).getClientProperty("JToggleButton.isTreeCellRenderer")); + return owner == null || treeEditor || treeRenderer; + } + return true; + } + @Override protected TreeCellEditor createDefaultCellEditor() { if (currentCellRenderer != null && (currentCellRenderer instanceof DarkTreeCellRenderer)) { @@ -332,7 +355,7 @@ public class DarkTreeUI extends BasicTreeUI implements PropertyChangeListener { @Override protected void uninstallListeners() { super.uninstallListeners(); - tree.removeMouseListener(mySelectionListener); + tree.removeMouseListener(selectionListener); tree.removePropertyChangeListener(this); } diff --git a/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java b/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java index db7c8282..0a4133da 100644 --- a/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java +++ b/src/main/java/com/github/weisj/darklaf/util/DarkUIUtil.java @@ -158,6 +158,7 @@ public final class DarkUIUtil { } public static boolean hasFocus(final Component c) { + if (c.hasFocus()) return true; if (c instanceof Window) { return hasFocus(c); }