Java Fx resizing scene to stage










0















I searched but could not find anything close to the problem that I am having with Fx. I am using Java Fx/JDK 8 and having problems with resizing the scene(s). The below code below loads only one screen at a time in scene graph, and switches between the screens. The problem is when I resize the stage. The scene is not being resized with the stage. Nothing fancy in FXML files only anchor pane and a few components.Thanks in advance.



public class ScreensFramework extends Application 

static String main = "main";
static String mainFile = "Main.fxml";
static String play = "play";
static String playFile = "Play.fxml";


public static void main(String args)
launch(args);


@Override
public void start(Stage mainStage)
ScreensController mainContainer = new ScreensController();
mainContainer.loadScreen(ScreensFramework.main, ScreensFramework.mainFile);
mainContainer.loadScreen(ScreensFramework.play, ScreensFramework.playFile);

mainContainer.setScreen(ScreensFramework.main);

AnchorPane root = new AnchorPane();
root.getChildren().setAll(mainContainer);
Scene scene = new Scene(root);
mainStage.setScene(scene);
//This did not work out either
mainStage.heightProperty().addListener((observable, oldValue, newValue) ->
root.getScene().prefHeight((double) newValue);
);
mainStage.show();



public class MainController implements Initializable, ControlledScreen

ScreensController myController;

@Override
public void initialize(URL url, ResourceBundle rb)


public void setScreenParent(ScreensController screenParent)
myController = screenParent;



public class PlayController implements Initializable, ControlledScreen

ScreensController myController;

@Override
public void initialize(URL url, ResourceBundle rb)


public void setScreenParent(ScreensController screenParent)
myController = screenParent;




public class ScreensController extends AnchorPane

private HashMap<String, Node> screens = new HashMap<>();

public void addScreen(String name, Node screen)
screens.put(name, screen);


public boolean loadScreen(String name, String resource)
try
FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
Parent loadScreen = (Parent) myLoader.load();
ControlledScreen myScreenControler = ((ControlledScreen) myLoader.getController());
myScreenControler.setScreenParent(this);
addScreen(name, loadScreen);
return true;
catch (Exception e)
System.out.println(e.getMessage());
return false;



public boolean setScreen(final String name)

if (screens.get(name) != null)
if (!getChildren().isEmpty())
getChildren().remove(0); //remove the displayed screen
getChildren().add(0, screens.get(name)); //add the screen

else
getChildren().add(screens.get(name));

return true;
else
return false;












share|improve this question






















  • There are series of issues in your code. It would be difficult to identify your problem by guessing the missing parts. Can you please provide a Minimal, Complete, and Verifiable example.

    – Sai Dandem
    Nov 14 '18 at 5:11











  • The info provided seems sufficient to identify the error. BTW: the remove+add calls for non-empty child lists in setScreen could be replaced with a set call. Furthermore I recommend storing the result of screens.get in a local variable to avoid multiple accesses to the map.

    – fabian
    Nov 14 '18 at 11:57
















0















I searched but could not find anything close to the problem that I am having with Fx. I am using Java Fx/JDK 8 and having problems with resizing the scene(s). The below code below loads only one screen at a time in scene graph, and switches between the screens. The problem is when I resize the stage. The scene is not being resized with the stage. Nothing fancy in FXML files only anchor pane and a few components.Thanks in advance.



public class ScreensFramework extends Application 

static String main = "main";
static String mainFile = "Main.fxml";
static String play = "play";
static String playFile = "Play.fxml";


public static void main(String args)
launch(args);


@Override
public void start(Stage mainStage)
ScreensController mainContainer = new ScreensController();
mainContainer.loadScreen(ScreensFramework.main, ScreensFramework.mainFile);
mainContainer.loadScreen(ScreensFramework.play, ScreensFramework.playFile);

mainContainer.setScreen(ScreensFramework.main);

AnchorPane root = new AnchorPane();
root.getChildren().setAll(mainContainer);
Scene scene = new Scene(root);
mainStage.setScene(scene);
//This did not work out either
mainStage.heightProperty().addListener((observable, oldValue, newValue) ->
root.getScene().prefHeight((double) newValue);
);
mainStage.show();



public class MainController implements Initializable, ControlledScreen

ScreensController myController;

@Override
public void initialize(URL url, ResourceBundle rb)


public void setScreenParent(ScreensController screenParent)
myController = screenParent;



public class PlayController implements Initializable, ControlledScreen

ScreensController myController;

@Override
public void initialize(URL url, ResourceBundle rb)


public void setScreenParent(ScreensController screenParent)
myController = screenParent;




public class ScreensController extends AnchorPane

private HashMap<String, Node> screens = new HashMap<>();

public void addScreen(String name, Node screen)
screens.put(name, screen);


public boolean loadScreen(String name, String resource)
try
FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
Parent loadScreen = (Parent) myLoader.load();
ControlledScreen myScreenControler = ((ControlledScreen) myLoader.getController());
myScreenControler.setScreenParent(this);
addScreen(name, loadScreen);
return true;
catch (Exception e)
System.out.println(e.getMessage());
return false;



public boolean setScreen(final String name)

if (screens.get(name) != null)
if (!getChildren().isEmpty())
getChildren().remove(0); //remove the displayed screen
getChildren().add(0, screens.get(name)); //add the screen

else
getChildren().add(screens.get(name));

return true;
else
return false;












share|improve this question






















  • There are series of issues in your code. It would be difficult to identify your problem by guessing the missing parts. Can you please provide a Minimal, Complete, and Verifiable example.

    – Sai Dandem
    Nov 14 '18 at 5:11











  • The info provided seems sufficient to identify the error. BTW: the remove+add calls for non-empty child lists in setScreen could be replaced with a set call. Furthermore I recommend storing the result of screens.get in a local variable to avoid multiple accesses to the map.

    – fabian
    Nov 14 '18 at 11:57














0












0








0








I searched but could not find anything close to the problem that I am having with Fx. I am using Java Fx/JDK 8 and having problems with resizing the scene(s). The below code below loads only one screen at a time in scene graph, and switches between the screens. The problem is when I resize the stage. The scene is not being resized with the stage. Nothing fancy in FXML files only anchor pane and a few components.Thanks in advance.



public class ScreensFramework extends Application 

static String main = "main";
static String mainFile = "Main.fxml";
static String play = "play";
static String playFile = "Play.fxml";


public static void main(String args)
launch(args);


@Override
public void start(Stage mainStage)
ScreensController mainContainer = new ScreensController();
mainContainer.loadScreen(ScreensFramework.main, ScreensFramework.mainFile);
mainContainer.loadScreen(ScreensFramework.play, ScreensFramework.playFile);

mainContainer.setScreen(ScreensFramework.main);

AnchorPane root = new AnchorPane();
root.getChildren().setAll(mainContainer);
Scene scene = new Scene(root);
mainStage.setScene(scene);
//This did not work out either
mainStage.heightProperty().addListener((observable, oldValue, newValue) ->
root.getScene().prefHeight((double) newValue);
);
mainStage.show();



public class MainController implements Initializable, ControlledScreen

ScreensController myController;

@Override
public void initialize(URL url, ResourceBundle rb)


public void setScreenParent(ScreensController screenParent)
myController = screenParent;



public class PlayController implements Initializable, ControlledScreen

ScreensController myController;

@Override
public void initialize(URL url, ResourceBundle rb)


public void setScreenParent(ScreensController screenParent)
myController = screenParent;




public class ScreensController extends AnchorPane

private HashMap<String, Node> screens = new HashMap<>();

public void addScreen(String name, Node screen)
screens.put(name, screen);


public boolean loadScreen(String name, String resource)
try
FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
Parent loadScreen = (Parent) myLoader.load();
ControlledScreen myScreenControler = ((ControlledScreen) myLoader.getController());
myScreenControler.setScreenParent(this);
addScreen(name, loadScreen);
return true;
catch (Exception e)
System.out.println(e.getMessage());
return false;



public boolean setScreen(final String name)

if (screens.get(name) != null)
if (!getChildren().isEmpty())
getChildren().remove(0); //remove the displayed screen
getChildren().add(0, screens.get(name)); //add the screen

else
getChildren().add(screens.get(name));

return true;
else
return false;












share|improve this question














I searched but could not find anything close to the problem that I am having with Fx. I am using Java Fx/JDK 8 and having problems with resizing the scene(s). The below code below loads only one screen at a time in scene graph, and switches between the screens. The problem is when I resize the stage. The scene is not being resized with the stage. Nothing fancy in FXML files only anchor pane and a few components.Thanks in advance.



public class ScreensFramework extends Application 

static String main = "main";
static String mainFile = "Main.fxml";
static String play = "play";
static String playFile = "Play.fxml";


public static void main(String args)
launch(args);


@Override
public void start(Stage mainStage)
ScreensController mainContainer = new ScreensController();
mainContainer.loadScreen(ScreensFramework.main, ScreensFramework.mainFile);
mainContainer.loadScreen(ScreensFramework.play, ScreensFramework.playFile);

mainContainer.setScreen(ScreensFramework.main);

AnchorPane root = new AnchorPane();
root.getChildren().setAll(mainContainer);
Scene scene = new Scene(root);
mainStage.setScene(scene);
//This did not work out either
mainStage.heightProperty().addListener((observable, oldValue, newValue) ->
root.getScene().prefHeight((double) newValue);
);
mainStage.show();



public class MainController implements Initializable, ControlledScreen

ScreensController myController;

@Override
public void initialize(URL url, ResourceBundle rb)


public void setScreenParent(ScreensController screenParent)
myController = screenParent;



public class PlayController implements Initializable, ControlledScreen

ScreensController myController;

@Override
public void initialize(URL url, ResourceBundle rb)


public void setScreenParent(ScreensController screenParent)
myController = screenParent;




public class ScreensController extends AnchorPane

private HashMap<String, Node> screens = new HashMap<>();

public void addScreen(String name, Node screen)
screens.put(name, screen);


public boolean loadScreen(String name, String resource)
try
FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
Parent loadScreen = (Parent) myLoader.load();
ControlledScreen myScreenControler = ((ControlledScreen) myLoader.getController());
myScreenControler.setScreenParent(this);
addScreen(name, loadScreen);
return true;
catch (Exception e)
System.out.println(e.getMessage());
return false;



public boolean setScreen(final String name)

if (screens.get(name) != null)
if (!getChildren().isEmpty())
getChildren().remove(0); //remove the displayed screen
getChildren().add(0, screens.get(name)); //add the screen

else
getChildren().add(screens.get(name));

return true;
else
return false;









java javafx javafx-8 javafx-2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 4:40









anupampunithanupampunith

6217




6217












  • There are series of issues in your code. It would be difficult to identify your problem by guessing the missing parts. Can you please provide a Minimal, Complete, and Verifiable example.

    – Sai Dandem
    Nov 14 '18 at 5:11











  • The info provided seems sufficient to identify the error. BTW: the remove+add calls for non-empty child lists in setScreen could be replaced with a set call. Furthermore I recommend storing the result of screens.get in a local variable to avoid multiple accesses to the map.

    – fabian
    Nov 14 '18 at 11:57


















  • There are series of issues in your code. It would be difficult to identify your problem by guessing the missing parts. Can you please provide a Minimal, Complete, and Verifiable example.

    – Sai Dandem
    Nov 14 '18 at 5:11











  • The info provided seems sufficient to identify the error. BTW: the remove+add calls for non-empty child lists in setScreen could be replaced with a set call. Furthermore I recommend storing the result of screens.get in a local variable to avoid multiple accesses to the map.

    – fabian
    Nov 14 '18 at 11:57

















There are series of issues in your code. It would be difficult to identify your problem by guessing the missing parts. Can you please provide a Minimal, Complete, and Verifiable example.

– Sai Dandem
Nov 14 '18 at 5:11





There are series of issues in your code. It would be difficult to identify your problem by guessing the missing parts. Can you please provide a Minimal, Complete, and Verifiable example.

– Sai Dandem
Nov 14 '18 at 5:11













The info provided seems sufficient to identify the error. BTW: the remove+add calls for non-empty child lists in setScreen could be replaced with a set call. Furthermore I recommend storing the result of screens.get in a local variable to avoid multiple accesses to the map.

– fabian
Nov 14 '18 at 11:57






The info provided seems sufficient to identify the error. BTW: the remove+add calls for non-empty child lists in setScreen could be replaced with a set call. Furthermore I recommend storing the result of screens.get in a local variable to avoid multiple accesses to the map.

– fabian
Nov 14 '18 at 11:57













1 Answer
1






active

oldest

votes


















1














You don't set the anchors for mainContainer or it's children. This way mainContainer simply is resized to it's preferred size and the children are resized to their preferred sizes. prefHeight() does not assign the preferred height, but calculates it.



Assigning preferred sizes is not necessary though, if you use the anchor properties:



AnchorPane.setTopAnchor(mainContainer, 0d);
AnchorPane.setRightAnchor(mainContainer, 0d);
AnchorPane.setBottomAnchor(mainContainer, 0d);
AnchorPane.setLeftAnchor(mainContainer, 0d);




public void addScreen(String name, Node screen) 
screens.put(name, screen);
AnchorPane.setTopAnchor(screen, 0d);
AnchorPane.setRightAnchor(screen, 0d);
AnchorPane.setBottomAnchor(screen, 0d);
AnchorPane.setLeftAnchor(screen, 0d);



Using StackPanes instead of AnchorPanes (both as scene root and as supertype of ScreensController) would be much simpler though, since it resizes the children without the need to specify anchors.






share|improve this answer

























  • That worked thanks.

    – anupampunith
    Nov 14 '18 at 19:50










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293294%2fjava-fx-resizing-scene-to-stage%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You don't set the anchors for mainContainer or it's children. This way mainContainer simply is resized to it's preferred size and the children are resized to their preferred sizes. prefHeight() does not assign the preferred height, but calculates it.



Assigning preferred sizes is not necessary though, if you use the anchor properties:



AnchorPane.setTopAnchor(mainContainer, 0d);
AnchorPane.setRightAnchor(mainContainer, 0d);
AnchorPane.setBottomAnchor(mainContainer, 0d);
AnchorPane.setLeftAnchor(mainContainer, 0d);




public void addScreen(String name, Node screen) 
screens.put(name, screen);
AnchorPane.setTopAnchor(screen, 0d);
AnchorPane.setRightAnchor(screen, 0d);
AnchorPane.setBottomAnchor(screen, 0d);
AnchorPane.setLeftAnchor(screen, 0d);



Using StackPanes instead of AnchorPanes (both as scene root and as supertype of ScreensController) would be much simpler though, since it resizes the children without the need to specify anchors.






share|improve this answer

























  • That worked thanks.

    – anupampunith
    Nov 14 '18 at 19:50















1














You don't set the anchors for mainContainer or it's children. This way mainContainer simply is resized to it's preferred size and the children are resized to their preferred sizes. prefHeight() does not assign the preferred height, but calculates it.



Assigning preferred sizes is not necessary though, if you use the anchor properties:



AnchorPane.setTopAnchor(mainContainer, 0d);
AnchorPane.setRightAnchor(mainContainer, 0d);
AnchorPane.setBottomAnchor(mainContainer, 0d);
AnchorPane.setLeftAnchor(mainContainer, 0d);




public void addScreen(String name, Node screen) 
screens.put(name, screen);
AnchorPane.setTopAnchor(screen, 0d);
AnchorPane.setRightAnchor(screen, 0d);
AnchorPane.setBottomAnchor(screen, 0d);
AnchorPane.setLeftAnchor(screen, 0d);



Using StackPanes instead of AnchorPanes (both as scene root and as supertype of ScreensController) would be much simpler though, since it resizes the children without the need to specify anchors.






share|improve this answer

























  • That worked thanks.

    – anupampunith
    Nov 14 '18 at 19:50













1












1








1







You don't set the anchors for mainContainer or it's children. This way mainContainer simply is resized to it's preferred size and the children are resized to their preferred sizes. prefHeight() does not assign the preferred height, but calculates it.



Assigning preferred sizes is not necessary though, if you use the anchor properties:



AnchorPane.setTopAnchor(mainContainer, 0d);
AnchorPane.setRightAnchor(mainContainer, 0d);
AnchorPane.setBottomAnchor(mainContainer, 0d);
AnchorPane.setLeftAnchor(mainContainer, 0d);




public void addScreen(String name, Node screen) 
screens.put(name, screen);
AnchorPane.setTopAnchor(screen, 0d);
AnchorPane.setRightAnchor(screen, 0d);
AnchorPane.setBottomAnchor(screen, 0d);
AnchorPane.setLeftAnchor(screen, 0d);



Using StackPanes instead of AnchorPanes (both as scene root and as supertype of ScreensController) would be much simpler though, since it resizes the children without the need to specify anchors.






share|improve this answer















You don't set the anchors for mainContainer or it's children. This way mainContainer simply is resized to it's preferred size and the children are resized to their preferred sizes. prefHeight() does not assign the preferred height, but calculates it.



Assigning preferred sizes is not necessary though, if you use the anchor properties:



AnchorPane.setTopAnchor(mainContainer, 0d);
AnchorPane.setRightAnchor(mainContainer, 0d);
AnchorPane.setBottomAnchor(mainContainer, 0d);
AnchorPane.setLeftAnchor(mainContainer, 0d);




public void addScreen(String name, Node screen) 
screens.put(name, screen);
AnchorPane.setTopAnchor(screen, 0d);
AnchorPane.setRightAnchor(screen, 0d);
AnchorPane.setBottomAnchor(screen, 0d);
AnchorPane.setLeftAnchor(screen, 0d);



Using StackPanes instead of AnchorPanes (both as scene root and as supertype of ScreensController) would be much simpler though, since it resizes the children without the need to specify anchors.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 11:59

























answered Nov 14 '18 at 11:52









fabianfabian

51.8k115272




51.8k115272












  • That worked thanks.

    – anupampunith
    Nov 14 '18 at 19:50

















  • That worked thanks.

    – anupampunith
    Nov 14 '18 at 19:50
















That worked thanks.

– anupampunith
Nov 14 '18 at 19:50





That worked thanks.

– anupampunith
Nov 14 '18 at 19:50

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293294%2fjava-fx-resizing-scene-to-stage%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto