2

I'm extremely new to programming. I'm using JavaFX and NetBeans IDE 8.0.2 to write a simple math program. I'm trying to change scenes in the same stage, and my code works but the stage will not stay maximized once the scenes have changed. I've tried everything i could think of to keep it maximized or to restore it to maximized i.e. stage.setMaximized(true); after the next scene is switched, but none of the code is working. I created a simple example of my problem. Does anyone have any tips for me in anyway? Thank you.

public class ProblemExample extends Application
{

    final double WIDTH = 600;
    final double HEIGHT = 600;

    Stage stage;
    Scene scene1, scene2;
    Pane pane1, pane2;

    public static void main(String[] args)
    {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage)
    {
        stage = primaryStage;
        pane1 = new Pane();
        pane2 = new Pane();

        getuiPane1();
        getuiPane2();

        scene1 = new Scene(pane1, WIDTH, HEIGHT);
        scene2 = new Scene(pane2, WIDTH, HEIGHT);
        stage.setTitle("Example");
        stage.setScene(scene1);
        stage.setMaximized(true);
        stage.show();
    } 

    public void getuiPane1()
    {
        Text nextText = new Text(300, 300, "Next >>");
        pane1.getChildren().add(nextText);

        nextText.setOnMouseClicked(e ->
        {
            if (e.getSource() == nextText)
            {
                stage.setScene(scene2);
            } else
            {
                stage.setScene(scene1);
            }
        }
        );
    }

    public void getuiPane2()
    {
        Text backText = new Text(300, 300, "<< Back");
        pane2.getChildren().add(backText);

        backText.setOnMouseClicked(e ->
        {
            if (e.getSource() == backText)
            {
                stage.setScene(scene1);
            } else
            {
                stage.setScene(scene2);
            }
        }
        );
    }
}
Lrrr
  • 4,755
  • 5
  • 41
  • 63
James
  • 21
  • 2
  • Have you tried just changing the root object of the existing scene, rather than making a new scene? – Evan Knowles Jun 15 '15 at 05:44
  • I haven't tried that. I tried to look up more information on it, but nothing seemed to fit what I was trying to do, or I just didn't understand it. You wouldn't happen to have a link to something or possibly an example would you? Thank you Evan. – James Jun 17 '15 at 00:38
  • @EvanKnowles thank you again for your suggestion. I figured it out though. I just needed to add stage.sizeToScene(); then toggle the setMaximized stage.setMaximized(false); stage.setMaximized(true); It works just fine now. – James Jun 22 '15 at 01:27

0 Answers0