Java FX crash when changing scene - java

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();
}

Related

JavaFx: ImageView disappears after moving through the scene

I have a code written using javafx and java 11. As I showed in the below code snippet, when I move my ImageView to right of the scene, The ImageView disappears.
At first:
and then after moving it using arrow keys with the help of scene event listener:
then:
and finally:
I don't use fxml. Here is my demo which has the problem too.
public class HelloApplication extends Application {
public String fetchResource(String path) {
return Objects.requireNonNull(getClass().getResource(path)).toString();
}
#Override
public void start(Stage stage) throws IOException {
Rectangle r = new Rectangle(100, 100);
ImageView spaceShip = new ImageView(fetchResource("spaceShip.png"));
spaceShip.setFitHeight(100);
spaceShip.setFitWidth(100);
spaceShip.setX(0);
spaceShip.setY(0);
EventHandler<KeyEvent> keyListener = event -> {
if (event.getCode() == KeyCode.RIGHT) {
spaceShip.setX(spaceShip.getX() + 20);
}
};
Group game = new Group(spaceShip);
Scene scene = new Scene(game, 1840, 1080);
scene.setOnKeyPressed(keyListener);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
I should mention that I create a rectangle and check the scenario with that, And there was not any problem. I think there is some problem with ImageView.
I couldn't reproduce the issue with your code, but I could with SceneBuilder. You may get the desired result if you wrap the Group in a Pane (at least it works in SceneBuilder).
Group game = new Group(spaceShip);
Pane pane = new Pane(game);
Scene scene = new Scene(pane, 1840, 1080);
stage.setScene(scene);
stage.show();

JAVAFX secondary panels

Hi guys a little programmer java Swing and I'm trying a new technology like JavaFx just that I just can not figure out how to make operations for navigation between views, in particular.
How can I replace a main view on the stage? for example I have a view that I associate with the calback start method in the scene in the following way
#Override
public void start(Stage primaryStage) {
vistaPrincipale.load();
Scene scene = new Scene(vistaPrincipale.getRoot());
primaryStage.setScene(scene);
primaryStage.show();
}
and during the execution of the program I would like to change the view one with the view two, only that I could not really understand how I did not even find enough material to solve the problem, in swing it was enough to change the frame content bread
another problem that I have encountered is to launch a secondary panel like a jdialog in swing, I solved this problem by creating a new stage and using it in the following way, but to be honest it really seems like a very bad solution
public class InfoAutori {
private static final Logger LOGGER = LoggerFactory.getLogger(InfoAutori.class);
private Stage stage;
public void init(){
FXMLLoader load = new FXMLLoader();
Parent root = new AnchorPane();
try {
root = load.load(getClass().getResourceAsStream("InfoAutori.fxml"));
} catch (Exception e) {
LOGGER.error("Si e' verificato un errore del tipo: " +
e.getLocalizedMessage());
e.printStackTrace();
}
stage = new Stage();
Scene scene = new Scene(root);
stage.setScene(scene);
}
public void visualizza(){
stage.showAndWait();
}
}

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);

Using standalone methods for each scene in JavaFX

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();
}

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.

Categories

Resources