From: Niki Roo Date: Fri, 1 May 2020 20:43:38 +0000 (+0200) Subject: ListModel: fix tooltip that doesn't disappear X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=143d16e3caae370e6108aeb77680d11308d10e08;p=fanfix-swing.git ListModel: fix tooltip that doesn't disappear --- diff --git a/ui/ListModel.java b/ui/ListModel.java index 137897ae..12f6aa62 100644 --- a/ui/ListModel.java +++ b/ui/ListModel.java @@ -251,7 +251,7 @@ public class ListModel extends DefaultListModel6 { */ @SuppressWarnings({ "unchecked", "rawtypes" }) // JList not in Java 1.6 public ListModel(final JList list, final JPopupMenu popup, - TooltipCreator tooltipCreator) { + final TooltipCreator tooltipCreator) { this.list = list; this.tooltipCreator = tooltipCreator; @@ -276,14 +276,9 @@ public class ListModel extends DefaultListModel6 { fireElementChanged(oldIndex); fireElementChanged(index); - synchronized (tooltipWatcher) { - if (tooltip != null) { - tooltip.setVisible(false); - } - tooltip = null; - } + if (tooltipCreator != null) { + showTooltip(null); - if (ListModel.this.tooltipCreator != null) { tooltipWatcher.delay("tooltip", new SwingWorker() { @Override @@ -294,12 +289,7 @@ public class ListModel extends DefaultListModel6 { @Override protected void done() { - synchronized (tooltipWatcher) { - if (tooltip != null) { - tooltip.setVisible(false); - } - tooltip = null; - } + showTooltip(null); if (index < 0 || index != hoveredIndex) { @@ -311,12 +301,7 @@ public class ListModel extends DefaultListModel6 { return; } - synchronized (tooltipWatcher) { - if (tooltip != null) { - tooltip.setVisible(false); - } - tooltip = newTooltip(index, me); - } + showTooltip(newTooltip(index, me)); } }); } @@ -358,12 +343,7 @@ public class ListModel extends DefaultListModel6 { list.locationToIndex(e.getPoint())); } - Window oldTooltip = tooltip; - tooltip = null; - if (oldTooltip != null) { - oldTooltip.setVisible(false); - } - + showTooltip(null); popup.show(list, e.getX(), e.getY()); } } @@ -581,9 +561,7 @@ public class ListModel extends DefaultListModel6 { private Window newTooltip(final int index, final MouseEvent me) { final T value = ListModel.this.get(index); - final Window newTooltip = tooltipCreator.generateTooltip(value, true); - if (newTooltip != null) { newTooltip.addMouseListener(new MouseAdapter() { @Override @@ -595,18 +573,33 @@ public class ListModel extends DefaultListModel6 { me.getYOnScreen()); promotedTooltip.setVisible(true); } - + newTooltip.setVisible(false); } }); newTooltip.setLocation(me.getXOnScreen(), me.getYOnScreen()); - newTooltip.setVisible(true); + showTooltip(newTooltip); } return newTooltip; } + private void showTooltip(Window tooltip) { + synchronized (tooltipCreator) { + if (this.tooltip != null) { + this.tooltip.setVisible(false); + this.tooltip.dispose(); + } + + this.tooltip = tooltip; + + if (tooltip != null) { + tooltip.setVisible(true); + } + } + } + /** * Generate a {@link ListCellRenderer} that supports {@link Hoverable} * elements.