From 2a92cf977ee2ae37d8302a294c6338fa51a5ca45 Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Sat, 2 Mar 2019 05:40:42 -0600 Subject: [PATCH] #38 fix Swing deadlock --- src/jexer/backend/SwingComponent.java | 42 ++++++++++++++++++++------- src/jexer/backend/SwingTerminal.java | 6 ++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/jexer/backend/SwingComponent.java b/src/jexer/backend/SwingComponent.java index 92fd1d89..23b50ab3 100644 --- a/src/jexer/backend/SwingComponent.java +++ b/src/jexer/backend/SwingComponent.java @@ -45,6 +45,7 @@ import java.awt.image.BufferedImage; import java.awt.image.BufferStrategy; import javax.swing.JComponent; import javax.swing.JFrame; +import javax.swing.SwingUtilities; /** * Wrapper for integrating with Swing, because JFrame and JComponent have @@ -363,17 +364,38 @@ class SwingComponent { * @param height the new height in pixels */ public void setDimensions(final int width, final int height) { - // Figure out the thickness of borders and use that to set the final - // size. - if (frame != null) { - Insets insets = getInsets(); - frame.setSize(width + insets.left + insets.right, - height + insets.top + insets.bottom); - } else { - Insets insets = getInsets(); - component.setSize(width + insets.left + insets.right, - height + insets.top + insets.bottom); + if (SwingUtilities.isEventDispatchThread()) { + // We are in the Swing thread and can safely set the size. + + // Figure out the thickness of borders and use that to set the + // final size. + if (frame != null) { + Insets insets = getInsets(); + frame.setSize(width + insets.left + insets.right, + height + insets.top + insets.bottom); + } else { + Insets insets = getInsets(); + component.setSize(width + insets.left + insets.right, + height + insets.top + insets.bottom); + } + return; } + + SwingUtilities.invokeLater(new Runnable() { + public void run() { + // Figure out the thickness of borders and use that to set + // the final size. + if (frame != null) { + Insets insets = getInsets(); + frame.setSize(width + insets.left + insets.right, + height + insets.top + insets.bottom); + } else { + Insets insets = getInsets(); + component.setSize(width + insets.left + insets.right, + height + insets.top + insets.bottom); + } + } + }); } /** diff --git a/src/jexer/backend/SwingTerminal.java b/src/jexer/backend/SwingTerminal.java index 43afc161..67211a6c 100644 --- a/src/jexer/backend/SwingTerminal.java +++ b/src/jexer/backend/SwingTerminal.java @@ -1935,6 +1935,12 @@ public class SwingTerminal extends LogicalScreen return; } + if (sessionInfo == null) { + // This is the initial component resize in construction, bail + // out. + return; + } + // Drop a new TResizeEvent into the queue sessionInfo.queryWindowSize(); synchronized (eventQueue) { -- 2.27.0