From c46be99c8c5ef754ea32249200b644ec2829fc3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Strau=C3=9F?= Date: Sat, 21 May 2016 16:50:16 +0200 Subject: [PATCH] fix channel grid previews sometimes flickering/briefly showing wrong image --- .../eu/over9000/skadi/ui/cells/ChannelGridCell.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/over9000/skadi/ui/cells/ChannelGridCell.java b/src/main/java/eu/over9000/skadi/ui/cells/ChannelGridCell.java index 39f9bfd..9ae3119 100644 --- a/src/main/java/eu/over9000/skadi/ui/cells/ChannelGridCell.java +++ b/src/main/java/eu/over9000/skadi/ui/cells/ChannelGridCell.java @@ -31,6 +31,7 @@ import javafx.beans.value.WeakChangeListener; import javafx.geometry.Insets; import javafx.scene.control.Label; +import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.MouseButton; import javafx.scene.layout.VBox; @@ -50,6 +51,9 @@ public class ChannelGridCell extends GridCell { private final VBox vBox; private final ChannelGrid grid; + private Channel lastItem; + private WeakChangeListener weakPreviewListener; + public ChannelGridCell(final ChannelGrid grid, final MainWindow mainWindow) { this.grid = grid; @@ -105,6 +109,11 @@ private void doSelectionUpdate(final ChannelGrid grid, final MainWindow mainWind protected void updateItem(final Channel item, final boolean empty) { super.updateItem(item, empty); + if (lastItem != null && !lastItem.equals(item)) { + lastItem.previewProperty().removeListener(weakPreviewListener); + } + lastItem = item; + if (empty || item == null) { imageView.setImage(null); setGraphic(null); @@ -130,7 +139,8 @@ protected void updateItem(final Channel item, final boolean empty) { game.textProperty().bind(item.gameProperty()); game.setGraphic(GlyphsDude.createIcon(FontAwesomeIcon.GAMEPAD)); - item.previewProperty().addListener(new WeakChangeListener<>((observable, oldValue, newValue) -> imageView.setImage(newValue))); + weakPreviewListener = new WeakChangeListener<>((observable, oldValue, newValue) -> imageView.setImage(newValue)); + item.previewProperty().addListener(weakPreviewListener); imageView.setImage(item.getPreview());