Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8094951

[Stage, Mac] Window that lost focus due to another stage being shown does not regain focus when clicked

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • 8u40
    • 8u5
    • javafx
    • None
    • Mac OS 10.9.3, Retina MBP

      Execute the following code and select the context menu item on the table. Another stage window opens (with the main window as owner). Then close that window using the window controls. After that the main window does not regain focus which you can see by the selection color of the table. Then click on the main window. It does not regain focus even then. It does regain focus, however, when I click on another window first and then on the main window of the application.


      import javafx.application.Application;
      import javafx.beans.property.ReadOnlyObjectWrapper;
      import javafx.collections.ObservableList;
      import javafx.scene.Node;
      import javafx.scene.Scene;
      import javafx.scene.control.ContextMenu;
      import javafx.scene.control.Label;
      import javafx.scene.control.MenuItem;
      import javafx.scene.control.SelectionMode;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableView;
      import javafx.scene.layout.StackPane;
      import javafx.stage.Stage;

      public class TableFocusExample extends Application {

          @Override
          public void start(Stage primaryStage) throws Exception {
              final StackPane basePane = new StackPane();
              basePane.getChildren().add(buildTable());
              final Scene scene = new Scene(basePane);
              primaryStage.setScene(scene);
              primaryStage.setTitle(getClass().getSimpleName());
              primaryStage.sizeToScene();
              primaryStage.show();
          }

          private Node buildTable() {
              final TableView<String> table = new TableView<>();
              final ContextMenu menu = new ContextMenu();
              final MenuItem item = new MenuItem("Show Info");
              item.setOnAction((e) -> {
                  final ObservableList<String> items = table.getSelectionModel().getSelectedItems();
                  final String text = "Selected " + items;
                  Stage msgDialog = new Stage();
                  msgDialog.initOwner(((MenuItem)e.getTarget()).getParentPopup().getOwnerWindow());
                  msgDialog.setScene(new Scene(new Label(text)));
                  msgDialog.sizeToScene();
                  msgDialog.show();
              });
              menu.getItems().add(item);
              table.setContextMenu(menu);
              TableColumn<String, String> col1 = new TableColumn<String, String>("Col1");
              col1.setCellValueFactory((features)-> new ReadOnlyObjectWrapper<String>(features.getValue()));
              table.getColumns().setAll(col1);
              table.getItems().addAll("One", "Two", "Three");

              return table;
          }

          public static void main(String[] args) {
              launch(args);
          }
      }

      I also had this exception once in a while when clicking into the window again after closing the second window that was opened by the context menu action:

      Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
      at javafx.scene.control.TableView$TableViewArrayListSelectionModel.handleSelectedCellsListChangeEvent(TableView.java:2657)
      at javafx.scene.control.TableView$TableViewArrayListSelectionModel.clearAndSelect(TableView.java:2180)
      at javafx.scene.control.TableView$TableViewArrayListSelectionModel.clearAndSelect(TableView.java:2140)
      at com.sun.javafx.scene.control.behavior.TableRowBehavior.doSelect(TableRowBehavior.java:196)
      at com.sun.javafx.scene.control.behavior.TableRowBehavior.mousePressed(TableRowBehavior.java:88)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
      at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
      at javafx.event.Event.fireEvent(Event.java:204)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3746)
      at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
      at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
      at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
      at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
      at com.sun.glass.ui.View.notifyMouse(View.java:898)

            anthony Anthony Petrov (Inactive)
            rkruegerjfx Robert Krueger (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: