Show windows JavaFX - java

i new in JavaFX.
I have a problem, when a view, called another ... i can see items on the desktop.
in other words, to click a button on the a.fxml view, it closes and opens the b.fxml view.
but in this transition, i can see the desktop of windows.
is possible what this may not happen ??
Stage stage = new Stage();
stage.setFullScreen(true);
Pane myPane = null;
FXMLLoader fxmlLoader;
fxmlLoader = new FXMLLoader(getClass().getResource(rutaAbrir));
myPane = (Pane)fxmlLoader.load();
Scene scene = new Scene(myPane);
stage.setScene(scene);
General controller = (General) fxmlLoader.getController();
controller.setPrevStage(stage);
stage.show();
Thanks.

Related

Changing stage width/height then switching scene does not update the UI in the new scene

I have two scenes A and B and a stage.
If I'm in scene A, and change the stage width / height, it will update the UI to scale it to the new stage dimensions.
However if from there I switch to scene B, the UI in scene B will not be updated till I force a update by doing something like resizing the screen or opening and closing the stage.
Is there a way to update all scenes when the stage width / height is changed? Since only one scene can be set to the stage at one time the current changes are not applying automatically to all scenes.
Here's a minimal reproduced example.
#Override
public void start(Stage stage) throws Exception {
stage.setWidth(400);
stage.setHeight(400);
Scene scene1, scene2;
BorderPane layout1 = new BorderPane();
Button buttontosecond = new Button("go to second page");
layout1.setCenter(buttontosecond);
scene1 = new Scene(layout1, stage.getWidth(), stage.getHeight());
BorderPane layout2 = new BorderPane();
Button buttonsize = new Button("change size");
Button buttontomain = new Button("back to main");
VBox vbox = new VBox(buttonsize, buttontomain);
vbox.setAlignment(Pos.CENTER);
layout2.setCenter(vbox);
scene2 = new Scene(layout2, stage.getWidth(), stage.getHeight());
buttontomain.setOnAction(actionEvent -> {stage.setScene(scene1);});
buttontosecond.setOnAction(actionEvent -> {stage.setScene(scene2);});
buttonsize.setOnAction(actionEvent -> {stage.setWidth(200);stage.setHeight(200);});
stage.setScene(scene1);
stage.show();
}
DO not do scene switching in this way. Instead implement a single scene root-switching system as shown in the answer to this post. I did not manage to get the code above to work, but I found that the reason for this was my dual monitor setup. When dragging the game window into the other monitor it worked fine.

How do you combine two scenes into one scene?

Is there a way to combine two javaFx scenes into one scene (then assign that scene to a stage) or, assign two scenes to a stage simultaneously so they are side by side on a stage.
Aim: I have a scene that shows a calculator, I have a scene that shows a clock. I want to have them side by side (calculator on left, clock on right) on the same stage (without using scene builder).
Any help would be appreciated.
yes you can do this in javafx with subscenes,
a subscene is like a scene that can be added into layouts
you can do something like this
#Override
public void start(Stage primaryStage) throws Exception {
StackPane layoutOne = new StackPane();
Label labelOne = new Label("One");
layoutOne.getChildren().add(labelOne);
SubScene subSceneOne = new SubScene(layoutOne,300,100);
StackPane layoutTwo = new StackPane();
Label labelTwo = new Label("Two");
layoutTwo.getChildren().add(labelTwo);
SubScene subSceneTwo = new SubScene(layoutTwo,300,100);
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(subSceneOne,subSceneTwo);
Scene mainScene = new Scene(root,300,210);
primaryStage.setScene(mainScene);
primaryStage.show();
}
hope this what you were looking for, you can also check this for more

JavaFX fullscreen stage resizing

I have a problem with fullscreen stages. I have 1 main stage which enters full screen immediately when application starts, then when the user invokes specific event, I create second stage which is also full screen, but it shows image and its background is transparent, so the main stage and its content is still visible.
The problem is when I display this new stage and set it to fullscreen mode, it makes my main stage exit its fullscreen mode and resize.
Creation of main stage:
root = new StackPane();
scene = new Scene(root);
stage = primaryStage;
scene.setOnKeyReleased(event -> {
if (event.getCode() == KeyCode.ESCAPE) {
close();
}
});
stage.setScene(scene);
stage.setFullScreen(true);
stage.setFullScreenExitHint(null);
stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
stage.setOnCloseRequest(closeRequest -> onClose(closeRequest));
Creation of the new stage:
root = new StackPane();
scene = new Scene(root);
stage = new Stage(StageStyle.TRANSPARENT);
image = new ImageView(new Image(imagePath, 400, 580, true, true));
root.setAlignment(Pos.CENTER);
root.setStyle("-fx-background-color: rgba(0, 0, 0, 0.5)");
root.getChildren().add(image);
scene.setFill(Color.TRANSPARENT);
scene.setOnMousePressed(event -> stage.close());
stage.initOwner(Window.getInstance().getStage());
stage.setScene(scene);
stage.setFullScreen(true);
stage.setFullScreenExitHint(null);
stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
Well I solved this problem by using stage.setMaximized(true) instead of setting it to fullscreen and it worked. I don't know if this is good solution, but I just wanted to post it here.

In Javafx when a window is opened how can i set it so that the user isnt able to open the same window again?

I tried a lot but just couldn't find any solution. At the moment the opened window(popup window) is always on top but the user can still access the main window. That's how it should be, but it shouldn't be possible to open the same popup window again.
Stage stage = new Stage();
stage.setTitle(panelTitle);
stage.setScene(new Scene(root));
stage.initModality(Modality.WINDOW_MODAL);
stage.setAlwaysOnTop(true);
stage.showAndWait();
Thank you in advance!
As LazerBanana said, I would disable the button that opens the window, and I would enable it when you close it.
Stage stage = new Stage();
button.setDisable(true);
stage.setTitle(panelTitle);
stage.setScene(new Scene(root));
stage.initModality(Modality.WINDOW_MODAL);
stage.setAlwaysOnTop(true);
stage.showAndWait();
// your logic here
button.setDisable(false);
An alternative solution to creating a new one each time is to create one and just setup and show.
public class Stack extends Application {
private final Stage popup = new Stage();
#Override
public void start(Stage stage) throws Exception {
BorderPane root = new BorderPane();
root.setPrefWidth(400);
root.setPrefHeight(200);
Button button = new Button("ClickMePopup");
root.setCenter(button);
button.setOnAction(
event -> {
if (!popup.isShowing()) {
// you dont set modality because after the stage is set to visible second time it will throw an exception.
// Again depends on what you need.
// popup.initModality(Modality.WINDOW_MODAL);
// this focuses the popup and main window is not clickable
// popup.initOwner(stage);
VBox dialogVbox = new VBox(20);
dialogVbox.getChildren().add(new Text("Some Dialog"));
Scene dialogScene = new Scene(dialogVbox, 300, 200);
popup.setScene(dialogScene);
// you can actually put all above into the method called initPopup() or whatever, do it once, and just show it here or just bind the property to the button.
popup.show();
}
});
Scene scene = new Scene(root);
stage.setTitle("Stack");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Or disable the button when clicked, but if your popup is not driven by the button or can be opened from other places the first idea would be a bit better in my opinion. Depends on what you need.
Or just create your own class and Springify it.

Java FX Disable Stage Maximisation Animation

When I open a new window in my application and maximise it using newStage.setMaximized(true); it shows the animation of the window increasing in size from the default size to the size of the screen, is there any way to disable this animation and swiftly move from one fullscreen window to another upon button click?
Here's my code:
public class HomeController {
#FXML private StackPane ap;
public void ButtonClicked(ActionEvent e) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("PitchRecognition.fxml"));
Parent root = (Parent)loader.load();
Stage stage = (Stage) ap.getScene().getWindow();
stage.hide();
Stage newStage = new Stage();
Scene scene = new Scene(root);
newStage.setScene(scene);
newStage.setMaximized(true);
newStage.show();
}
}

Categories

Resources