Posted By

SwiftWinds on 01/26/19


Tagged


Versions (?)

requesting focus on JavaFX stage


 / Published in: Java
 

Demonstrates that requesting focus on macOS doesn't work, even with useSystemMenuBar()

  1. package me.matetoes.libdockvisibility;
  2.  
  3. import javafx.application.Application;
  4. import javafx.application.Platform;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Button;
  7. import javafx.scene.control.Menu;
  8. import javafx.scene.control.MenuBar;
  9. import javafx.scene.layout.BorderPane;
  10. import javafx.stage.Stage;
  11.  
  12. import javax.imageio.ImageIO;
  13. import java.awt.*;
  14. import java.awt.event.ActionListener;
  15. import java.io.IOException;
  16. import java.net.URL;
  17.  
  18. ;
  19.  
  20. public class StackOverflowDockVisibility extends Application {
  21.  
  22.  
  23. private Button hideButton;
  24.  
  25. public static void main(String[] args) {
  26. launch(args);
  27. }
  28.  
  29. @Override
  30. public void start(Stage primaryStage) {
  31. hideButton = new javafx.scene.control.Button("Hide");
  32. hideButton.setOnAction(e -> handleHide());
  33. primaryStage.setTitle("Testing");
  34. Platform.setImplicitExit(false);
  35. createTrayIcon(primaryStage);
  36.  
  37. MenuBar menuBar = new MenuBar();
  38. final String os = System.getProperty("os.name");
  39. if (os != null && os.startsWith("Mac")) {
  40. menuBar.useSystemMenuBarProperty().set(true);
  41. }
  42.  
  43. final javafx.scene.control.MenuItem menuItem = new javafx.scene.control.MenuItem("test");
  44.  
  45. Menu[] menus = new Menu[3];
  46.  
  47. menus[0] = new Menu("File");
  48. menus[1] = new Menu("Options");
  49. menus[2] = new Menu("Help");
  50.  
  51. menus[2].getItems().add(menuItem);
  52.  
  53. menuBar.getMenus().addAll(menus);
  54.  
  55. BorderPane borderPane = new BorderPane();
  56. borderPane.setTop(menuBar);
  57. borderPane.setCenter(hideButton);
  58.  
  59. primaryStage.setScene(new Scene(borderPane));
  60.  
  61. primaryStage.show();
  62. }
  63.  
  64. private void createTrayIcon(final Stage stage) {
  65. if (SystemTray.isSupported()) {
  66. SystemTray tray = SystemTray.getSystemTray(); // get the SystemTray instance
  67.  
  68. Image icon = null;
  69. try { // load an image
  70. URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
  71. icon = ImageIO.read(url);
  72. } catch (IOException ex) {
  73. ex.printStackTrace();
  74. }
  75.  
  76. stage.setOnCloseRequest(e -> hide(stage)); //hide instead of close
  77.  
  78. // to be added on "show" MenuItem and trayIcon itself
  79. ActionListener showListener = e -> show(stage);
  80.  
  81. PopupMenu popup = new PopupMenu(); // create a popup menu
  82.  
  83. MenuItem showItem = new MenuItem("Show");
  84. showItem.addActionListener(showListener);
  85.  
  86. MenuItem closeItem = new MenuItem("Close");
  87. closeItem.addActionListener(e -> System.exit(0));
  88.  
  89. popup.add(showItem);
  90. popup.addSeparator();
  91. popup.add(closeItem);
  92.  
  93. assert icon != null;
  94. TrayIcon trayIcon = new TrayIcon(icon, "Test", popup); // construct a TrayIcon
  95. trayIcon.setImageAutoSize(true);
  96. trayIcon.addActionListener(showListener);
  97.  
  98. try { // add the tray image
  99. tray.add(trayIcon);
  100. } catch (AWTException e) {
  101. e.printStackTrace();
  102. }
  103. }
  104. }
  105.  
  106. private void hide(final Stage stage) {
  107. Platform.runLater(() -> {
  108. if (SystemTray.isSupported()) {
  109. stage.hide();
  110. } else {
  111. System.exit(0);
  112. }
  113. });
  114. }
  115.  
  116. private void show(final Stage stage) {
  117. Platform.runLater(() -> {
  118. stage.show();
  119.  
  120. // doesn't work!
  121. stage.requestFocus();
  122. stage.toFront();
  123. });
  124. }
  125.  
  126. private void handleHide() {
  127. Stage stage = (Stage) hideButton.getScene().getWindow();
  128. hide(stage);
  129. }
  130. }

Report this snippet  

You need to login to post a comment.