Reputation: 51
Have an issue with JavaFX where whenI popup a new stage, that new window will take focus from any windows application with current focus
I want it to popup to the front, but not take focus, so if the user was typing elsewhere they can continue to type etc.
In Swing you could get around this by:
dialog.setFocusable(false);
dialog.setVisible(true);
dialog.setFocusable(true);
There seems to be no similar option in JavaFx.
Example below, when you click the button it will popup a new stage, taking focus (note I don't want to request focus back, as in the real application the user could be writing an email or on a web page when the popup happens, it needs to not take focus from these activities)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Main extends Application {
private Stage stage;
@Override public void start(Stage stage) {
this.stage = stage;
stage.setTitle("Main Stage");
stage.setWidth(500);
stage.setHeight(500);
Button btnPopupStage = new Button("Click");
btnPopupStage.setOnMouseClicked(event -> popupStage());
Scene scene = new Scene(btnPopupStage);
stage.setScene(scene);
stage.show();
}
private void popupStage(){
Stage subStage = new Stage();
subStage.setTitle("Sub Stage");
subStage.setWidth(250);
subStage.setHeight(250);
subStage.initOwner(stage);
subStage.show();
System.out.println("Does main stage have focus : "+stage.isFocused());
System.out.println("Does popup have focus : "+subStage.isFocused());
}
public static void main(String[] args) {
launch(args);
}
}
Any ideas for a stage to not take focus on a stage.show() ? Thanks
Upvotes: 2
Views: 3248
Reputation: 51
Just incase anyone is is searching, I couldn't find a solution, but I found someone with the same issue who raised a ticket for the openjdk https://bugs.openjdk.java.net/browse/JDK-8090742
To get around it unfortunately I put the JavaFX pane inside a Swing JFrame and just called
dialog.setFocusableWindowState(false);
dialog.setVisible(true);
dialog.setFocusableWindowState(true);
Which has fixed the focus stealing issue, but not sure on the effects of having JFx in a Swing JFrame.
If anyone finds a non focus stealing way of a new Window in JFx let me know.
Thanks
Upvotes: 1
Reputation: 10859
to prevent what you are describing by assuming is the cause of your problem then you need to add subStage.initModality(Modality.NONE);
. But that is not the problem, actually there is no problem here look here
stage.setWidth(500); // your stage is 500 wide
stage.setHeight(500); // & 500 long
Scene scene = new Scene(btnPopupStage);// your scene has a parent which is button
//by inheritance if i should say your button is 500 wide & long.
Have you seen the problem? your button will always respond to click events which in event will create a new Stage
hence give focus to that Stage
Upvotes: 0