package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
MenuBar bar = new MenuBar();
Menu file = new Menu("File");
MenuItem item = new MenuItem("Static Open Window");
item.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
openWindow(primaryStage);
}
});
file.getItems().add(item);
file.setOnShowing(new EventHandler<Event>() {
@Override
public void handle(Event event) {
MenuItem item = new MenuItem("Dynamic Open Window");
item.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
openWindow(primaryStage);
}
});
file.getItems().add(item);
}
});
bar.getMenus().add(file);
BorderPane p = new BorderPane();
p.setTop(bar);
p.setCenter(new TextField());
primaryStage.setScene(new Scene(p,500,500));
primaryStage.show();
}
private void openWindow(Stage primaryStage) {
Stage s = new Stage();
s.initModality(Modality.APPLICATION_MODAL);
s.initOwner(primaryStage);
s.setScene(new Scene(new BorderPane(new TextField()),200,200));
s.showAndWait();
}
public static void main(String[] args) {
launch(args);
}
}
Run code:
* click "Static Open Window" => type and input target is the new window text which is OK
* close window
* click one of the "Dynamic Open Window" => type and see that the input target is the old windows text field
* close and notice that you can not reopen the menu (you can fix this by moving the focus out of the window and back in)
This is a very serious bug for big applications (like Eclipse) who construct menus on demand
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
MenuBar bar = new MenuBar();
Menu file = new Menu("File");
MenuItem item = new MenuItem("Static Open Window");
item.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
openWindow(primaryStage);
}
});
file.getItems().add(item);
file.setOnShowing(new EventHandler<Event>() {
@Override
public void handle(Event event) {
MenuItem item = new MenuItem("Dynamic Open Window");
item.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
openWindow(primaryStage);
}
});
file.getItems().add(item);
}
});
bar.getMenus().add(file);
BorderPane p = new BorderPane();
p.setTop(bar);
p.setCenter(new TextField());
primaryStage.setScene(new Scene(p,500,500));
primaryStage.show();
}
private void openWindow(Stage primaryStage) {
Stage s = new Stage();
s.initModality(Modality.APPLICATION_MODAL);
s.initOwner(primaryStage);
s.setScene(new Scene(new BorderPane(new TextField()),200,200));
s.showAndWait();
}
public static void main(String[] args) {
launch(args);
}
}
Run code:
* click "Static Open Window" => type and input target is the new window text which is OK
* close window
* click one of the "Dynamic Open Window" => type and see that the input target is the old windows text field
* close and notice that you can not reopen the menu (you can fix this by moving the focus out of the window and back in)
This is a very serious bug for big applications (like Eclipse) who construct menus on demand
- relates to
-
JDK-8089848 Mac: wrong window may gain native focus
- Open