From e18e8e31580e098fe822d75b929813be51b4f912 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sun, 7 Jun 2020 15:25:11 +0200 Subject: [PATCH] Popup: made Popup.show(), hide() and component listener more robust when used in unusual ways (issue #106) --- .../formdev/flatlaf/ui/FlatPopupFactory.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPopupFactory.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPopupFactory.java index 37fc2c1f..08f479e0 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPopupFactory.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatPopupFactory.java @@ -153,7 +153,8 @@ public class FlatPopupFactory @Override public void show() { - delegate.show(); + if( delegate != null ) + delegate.show(); } @Override @@ -185,6 +186,7 @@ public class FlatPopupFactory private boolean oldOpaque; // medium weight + private boolean mediumWeightShown; private Panel mediumWeightPanel; private JPanel dropShadowPanel; private ComponentListener mediumPanelListener; @@ -311,6 +313,11 @@ public class FlatPopupFactory } private void showMediumWeightDropShadow() { + if( mediumWeightShown ) + return; + + mediumWeightShown = true; + Window window = SwingUtilities.windowForComponent( owner ); if( window == null ) return; @@ -326,24 +333,29 @@ public class FlatPopupFactory mediumPanelListener = new ComponentListener() { @Override public void componentShown( ComponentEvent e ) { - dropShadowPanel.setVisible( true ); + if( dropShadowPanel != null ) + dropShadowPanel.setVisible( true ); } @Override public void componentHidden( ComponentEvent e ) { - dropShadowPanel.setVisible( false ); + if( dropShadowPanel != null ) + dropShadowPanel.setVisible( false ); } @Override public void componentMoved( ComponentEvent e ) { - Point location = mediumWeightPanel.getLocation(); - Insets insets = dropShadowPanel.getInsets(); - dropShadowPanel.setLocation( location.x - insets.left, location.y - insets.top ); + if( dropShadowPanel != null && mediumWeightPanel != null ) { + Point location = mediumWeightPanel.getLocation(); + Insets insets = dropShadowPanel.getInsets(); + dropShadowPanel.setLocation( location.x - insets.left, location.y - insets.top ); + } } @Override public void componentResized( ComponentEvent e ) { - dropShadowPanel.setSize( FlatUIUtils.addInsets( mediumWeightPanel.getSize(), dropShadowPanel.getInsets() ) ); + if( dropShadowPanel != null ) + dropShadowPanel.setSize( FlatUIUtils.addInsets( mediumWeightPanel.getSize(), dropShadowPanel.getInsets() ) ); } }; mediumWeightPanel.addComponentListener( mediumPanelListener );