Using standalone methods for each scene in JavaFX - java

I'm trying to use my scene, home, as a scene in start.
However it does not work and instead of getting my 300 x 300, I get a blank 900 x 400 screen. Perhaps it's something very easily detectable but I'm not seeing it?
private Scene home;
private Stage window;
public Scene home(Scene home) {
// build my scene
return home = new Scene(root, 300, 300);
}
#Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setScene(home);
window.show();
}
I'm trying to create my scenes as methods so I can keep them out of start.
The plan is to switch between scenes later by using: btn.setOnAction(e -> window.setScene(anotherScene));, thanks in advance everyone!

You never call the home method. Therefore the home field will remain null which is the value you pass to window.setScene.
Furthermore I'd call the home method is implemented in a strange way:
public Scene home(Scene home) {
The parameter is never read.
return home = new Scene(root, 300, 300);
This assigns the value to the method parameter, not to the scene before it returns the scene, which has no effect, since java is call-by-value.
You could implement it like this:
private Scene home;
private Stage window;
public Scene home() {
if (home == null) {
// build my scene
home = new Scene(root, 300, 300)
// or maybe do this without testing, if the scene was created before???
}
return home;
}
#Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setScene(home()); // use the method here
window.show();
}

Related

Java FX crash when changing scene

I'm trying to make a POS using javaFX but whenever I try to change the scene it keeps crasing. Any ideas of how to fix?
Main class: https://pastebin.com/4bspSL9N
SceneManager: https://pastebin.com/SU99DVgf
OutputHelper.log() is just a system.out.println()
output log: https://pastebin.com/GZTPRgNp
You have not initialized your primaryStage in the SceneManager. Try adding this.primaryStage = primaryStage; in your setup method.
Try this in your SceneManager class:
public static void loadScreen(Parent parent) throws IOException {
Platform.runLater(() -> {
Stage stage = new Stage(StageStyle.UNDECORATED);
// stage.setMaximized(true);
stage.setFullScreen(true);
// stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
});
}
Then to Load new screen use it like :
String Screen = "YourScreen.fxml";
Parent root = FXMLLoader.load(getClass().getResource(Screen));
SceneManager.loadScreen(root);
hope this will help
This a bit late to the party but we will post for posterity
We name all our Anchor Pane's so we know who they are ie mynamePane
Then when you click a Button to go somewhere you only need to know where you are and where you want to go
private void doSEARCH() throws IOException{
stage = (Stage)searchPane.getScene().getWindow();// pane you are ON
viewtxPane = FXMLLoader.load(getClass().getResource("tableview.fxml"));// pane you are GOING TO
Scene scene = new Scene(viewtxPane);// pane you are GOING TO
scene.getStylesheets().add(getClass().getResource("checkbook.css").toExternalForm());
stage.setScene(scene);
stage.setTitle("Check Book Transactions");
stage.show();
stage.sizeToScene();
stage.centerOnScreen();
}

Bringing a single stage to the front in windows in JavaFX

I am trying to create a stage in JavaFX that can pop up to the front in windows, while the main stage stays minimized.
This only works however, when the main stage is visible on the screen. I have tried making it work using Modality, but then the user can't interact with the main stage, which is not what i want.
The problem can be reproduced with the following Application:
public class MainApp extends Application {
#Override
public void start(Stage stage) throws Exception {
Scene mainScene = new Scene(new Parent() {});
Stage mainStage = new Stage();
mainStage.setScene(mainScene);
mainStage.show();
mainStage.setIconified(true);
Scene popUpScene = new Scene(new Parent() {});
Stage popUpStage = new Stage();
popUpStage.setScene(popUpScene);
Thread.sleep(5000);
popUp(popUpStage);
}
public static void popUp(Stage popUpStage){
if (popUpStage.isIconified()) popUpStage.setIconified(false);
popUpStage.show();
popUpStage.requestFocus();
popUpStage.toFront();
}
}
Is there anyone who has an answer to this problem?
Just add these two line to popUp. First line brings it to front. Second line allows interaction with the main stage or other windows.
popUpStage.setAlwaysOnTop(true);
popUpStage.setAlwaysOnTop(false);

Javafx Scene switching causing stage size increase

I am using Javafx (without using FXML), and I am passing the stage into the a controller to change the scene on the stage when a button is clicked. The scene changes correctly but the size of the stage and the scene increases.It increases in size by about 0.1 (in the width) and the height sometimes also increases (not every time).
Here is the controller being used.
public class Controller {
public Controller(){
}
public static void homeButtonhandler(Stage stage){
stage.close();
}
public static void adminButtonhandler(Stage stage){
adminPane adminPane1 = new adminPane(stage);
Scene adminScene = new Scene (adminPane1);
stage.setScene(adminScene);
}}
The adminPane extends another class I created called mainPane which both extend the Pane class. Which have other panes within them to create the GUI structure. The sizing for the main pane is set up like so:
top = createTop(stage);
this.getChildren().addAll(top);
this.setWidth(stage.getWidth());
this.setPrefWidth(stage.getWidth());
this.setMaxWidth(stage.getWidth());
this.setMinWidth(stage.getWidth());
this.setHeight(stage.getHeight());
this.setMaxHeight(stage.getHeight());
this.setMinHeight(stage.getHeight());
this.setPrefHeight(stage.getHeight());
I am testing the classes using:
public class test extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
//primaryStage.setResizable(true);
mainPane myPane = new mainPane(primaryStage);
Scene homeScene = new Scene (myPane);
primaryStage.setScene(homeScene);
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/resources/icons/joulesIcon.png")));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I believe it has something to do with my passing of the stage, any pointers would be much appreciated.
I also found that stuff was being drawn below the bottom of the window when I used primaryStage.setScene(new Scene(root, primaryStage.getWidth(), primaryStage.getHeight())). My case was resolved by using the dimensions of the old scene rather than the dimensions of the primary stage.
public void setScene(Parent root) {
Scene oldScene = primaryStage.getScene();
primaryStage.setScene(oldScene == null
? new Scene(root, primaryStage.getMinWidth(), primaryStage.getMinHeight())
: new Scene(root, oldScene.getWidth(), oldScene.getHeight()));
}

Making Stage static in javaFX. is it a good idea?

I actually want to build a fx application that go from one fxml to another (with this process :fxml number 1 load then go with a click and another load).
I have made stage in my main class static and use it again and again.
according to Restriction of stack space in ram is it a good idea ?? or there is a better way?
these are parts of my code:
public class Main extends Application {
public static Stage stage;
#Override
public void start(Stage primaryStage) throws Exception{
stage=primaryStage;
Parent root = FXMLLoader.load(getClass().getResource("MainWidget.fxml"));
stage.setTitle("Welcome");
stage.setScene(new Scene(root, 300, 275));
stage.show();
}
and my controller is(loading another fxml!! )
public void someButtonController{
Parent root = FXMLLoader.load(getClass().getResource("/View/ShowWidget.fxml"));
Scene scene = new Scene(root,300,300);
Main.stage.setScene(scene);
Main.stage.show();}
I would not expose the Stage at all. Doing so couples your FXML-Controller pair to your Main class and prevents you ever using it without that class.
Instead, do something like
#FXML
private Button someButton ;
// ...
public void someButtonController{
Window window = someButton.getScene().getWindow();
if (window instanceof Stage) {
Parent root = FXMLLoader.load(getClass().getResource("/View/ShowWidget.fxml"));
Scene scene = new Scene(root,300,300);
Stage stage = (Stage) window ;
stage.setScene(scene);
stage.show(); // isn't it necessarily showing already?
}
}
Here I'm assuming that the controller is the controller for an FXML file representing Nodes that are displayed in the Stage you're trying to access. If that's not the case, you can still do something similar though it will be more complex.

New Stage in Javafx is coming to center

We are using more than one stage(window) in JavaFX for a desktop standalone application. So when we drag or move any of the stage(window) form one point to another and clicking a certain button to navigate to the other page means the new stage is coming to center again(default position). Is there any methods to set the future stage(future windows) also in the previous stage location or point(previous windows). Please help.
You can control stage location before showing it. Just remember last stage coordinates and create new one in the same point:
public class FXStages extends Application {
private int counter = 0;
#Override
public void start(final Stage stage) {
Button btn = new Button();
btn.setText("Reopen");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
stage.close();
Stage newStage = new Stage();
newStage.setX(stage.getX());
newStage.setY(stage.getY());
start(newStage);
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
stage.setTitle("Stage " + counter++);
stage.setScene(scene);
stage.show();
}
}

Categories

Resources