From 4614b3bf2220b2ddc27ae87aeb5ec893451d4e60 Mon Sep 17 00:00:00 2001 From: Kevin Lamonte Date: Sun, 18 Aug 2019 18:45:44 -0500 Subject: [PATCH] #47 setfont only in swing thread --- src/jexer/backend/SwingComponent.java | 12 +++++++++ src/jexer/backend/SwingTerminal.java | 38 +++++++++++++++++++++------ 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/jexer/backend/SwingComponent.java b/src/jexer/backend/SwingComponent.java index 57f8fd6..3d1074c 100644 --- a/src/jexer/backend/SwingComponent.java +++ b/src/jexer/backend/SwingComponent.java @@ -586,4 +586,16 @@ class SwingComponent { } } + /** + * Requests that this Component get the input focus, if this Component's + * top-level ancestor is already the focused Window. + */ + public void requestFocusInWindow() { + if (frame != null) { + frame.requestFocusInWindow(); + } else { + component.requestFocusInWindow(); + } + } + } diff --git a/src/jexer/backend/SwingTerminal.java b/src/jexer/backend/SwingTerminal.java index 7ca80a5..f0ba355 100644 --- a/src/jexer/backend/SwingTerminal.java +++ b/src/jexer/backend/SwingTerminal.java @@ -821,13 +821,35 @@ public class SwingTerminal extends LogicalScreen * @param font the new font */ public void setFont(final Font font) { - synchronized (this) { - this.font = font; - getFontDimensions(); - swing.setFont(font); - glyphCacheBlink = new HashMap(); - glyphCache = new HashMap(); - resizeToScreen(true); + if (!SwingUtilities.isEventDispatchThread()) { + // Not in the Swing thread: force this inside the Swing thread. + try { + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + synchronized (this) { + SwingTerminal.this.font = font; + getFontDimensions(); + swing.setFont(font); + glyphCacheBlink = new HashMap(); + glyphCache = new HashMap(); + resizeToScreen(true); + } + } + }); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (java.lang.reflect.InvocationTargetException e) { + e.printStackTrace(); + } + } else { + synchronized (this) { + SwingTerminal.this.font = font; + getFontDimensions(); + swing.setFont(font); + glyphCacheBlink = new HashMap(); + glyphCache = new HashMap(); + resizeToScreen(true); + } } } @@ -2156,7 +2178,7 @@ public class SwingTerminal extends LogicalScreen * @param mouse mouse event received */ public void mouseEntered(final MouseEvent mouse) { - // Ignore + swing.requestFocusInWindow(); } /** -- 2.27.0