From: Kevin Lamonte Date: Sun, 18 Aug 2019 23:45:44 +0000 (-0500) Subject: #47 setfont only in swing thread X-Git-Tag: fanfix-3.0.1^2~62 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=4614b3bf2220b2ddc27ae87aeb5ec893451d4e60;p=fanfix.git #47 setfont only in swing thread --- 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(); } /**