#47 setfont only in swing thread
authorKevin Lamonte <kevin.lamonte@gmail.com>
Sun, 18 Aug 2019 23:45:44 +0000 (18:45 -0500)
committerKevin Lamonte <kevin.lamonte@gmail.com>
Sun, 18 Aug 2019 23:45:44 +0000 (18:45 -0500)
src/jexer/backend/SwingComponent.java
src/jexer/backend/SwingTerminal.java

index 57f8fd6b40ee4a508bc907e9d50c4d69510aa853..3d1074cf889070d6bc1c7eee4d9be1da80b92d06 100644 (file)
@@ -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();
+        }
+    }
+
 }
index 7ca80a5573f59eeaf57fa8096ca592acc7770523..f0ba3552fd52b812a91a06be0f97c9adb96604e7 100644 (file)
@@ -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<Cell, BufferedImage>();
-            glyphCache = new HashMap<Cell, BufferedImage>();
-            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<Cell, BufferedImage>();
+                            glyphCache = new HashMap<Cell, BufferedImage>();
+                            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<Cell, BufferedImage>();
+                glyphCache = new HashMap<Cell, BufferedImage>();
+                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();
     }
 
     /**