Automatically resizing the window based on dynamic content












0















I am checking for a feature to automatically resize the window based on the content. I am already aware of Window.sizeToScene() method. But this is so cumbersome that I need to take care in every place to call the sizeToScene(). For example, when I add/remove nodes, when I expanded the titlePanes, etc...



Can someone let me know if it is possible to automatically adjust the window based on the content. I am looking this feature for both normal and transparent windows.



Any hints/suggestion is highly appreciated.



Please find below a quick demo of what I am looking for. Everything works as expected if I consider calling sizeToScene(). I am targeting for a way to get the same behavior without calling sizeToScene for every scenario.



enter image description here



import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class TransparentWindowResizeDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
VBox root = new VBox();
root.setSpacing(15);
root.setAlignment(Pos.CENTER);

Scene sc = new Scene(root, 300, 300);
stage.setScene(sc);
stage.show();

Button showNormal = new Button("Show Normal Window");
showNormal.setOnAction(e -> showWindow(false));

Button showTransparent = new Button("Show Transparent Window");
showTransparent.setOnAction(e -> showWindow(true));

root.getChildren().addAll(showNormal, showTransparent);
}

private void showWindow(boolean isTransparent) {
Stage window = new Stage();
VBox root = new VBox();
root.setStyle("-fx-background-color: grey; -fx-border-width:2px;-fx-border-color:black;");
Scene scene = new Scene(root, Color.TRANSPARENT);
window.setScene(scene);

VBox layout = new VBox();
layout.setSpacing(5);
layout.setPadding(new Insets(5));

CheckBox sizeToScene = new CheckBox("sizeToScene");
sizeToScene.setSelected(true);

StackPane box = new StackPane();
box.setStyle("-fx-background-color:yellow; -fx-border-width:1px;-fx-border-color:black;");
box.setMinSize(200, 100);
box.setMaxSize(200, 100);

Button add = new Button("Add Pane");
Button remove = new Button("Remove Pane");
remove.setDisable(true);
add.setOnAction(e -> {
layout.getChildren().add(box);
add.setDisable(true);
remove.setDisable(false);
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});
remove.setOnAction(e -> {
layout.getChildren().remove(box);
add.setDisable(false);
remove.setDisable(true);
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});
HBox btnLayout = new HBox();
btnLayout.setSpacing(5);
btnLayout.getChildren().addAll(add, remove);

StackPane tpContent = new StackPane();
tpContent.setStyle("-fx-background-color:pink; -fx-border-width:1px;-fx-border-color:black;");
tpContent.setMinSize(200, 100);
tpContent.setMaxSize(200, 100);

TitledPane tp = new TitledPane("Titled Pane", tpContent);
tp.setAnimated(false);
tp.expandedProperty().addListener((obs, oldVal, newVal) -> {
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});

if (isTransparent) {
window.initStyle(StageStyle.TRANSPARENT);

StackPane close = new StackPane();
close.setMaxWidth(30);
close.setStyle("-fx-background-color:red;");
close.getChildren().add(new Label("X"));
close.setOnMouseClicked(e -> window.hide());

DoubleProperty x = new SimpleDoubleProperty();
DoubleProperty y = new SimpleDoubleProperty();
StackPane header = new StackPane();
header.setOnMousePressed(e -> {
x.set(e.getSceneX());
y.set(e.getSceneY());
});
header.setOnMouseDragged(e -> {
window.setX(e.getScreenX() - x.get());
window.setY(e.getScreenY() - y.get());
});
header.setStyle("-fx-border-width:0px 0px 2px 0px;-fx-border-color:black;");
header.setMinHeight(30);
header.setAlignment(Pos.CENTER_RIGHT);
Label lbl = new Label("I am draggable !!");
lbl.setStyle("-fx-text-fill:white;-fx-font-weight:bold;");
StackPane.setAlignment(lbl,Pos.CENTER_LEFT);
header.getChildren().addAll(lbl,close);
root.getChildren().add(header);
}

layout.getChildren().addAll(sizeToScene, btnLayout, tp);
root.getChildren().add(layout);
window.show();

}

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


Edit :: This is different from what I already found in other questions or with the link in the comment. The solution that is specified is what I am aware of and I already mentioned that in the question. I have a heavy screen where it contain many nodes in differnt hirerchy. I am checking for any generic solution to include in one place rather that calling from every node add/remove scenario.










share|improve this question

























  • Possible duplicate of JavaFX Stage/Scene automatic resize

    – Sedrick
    Nov 28 '18 at 0:46











  • @Sedrick, I already mentioned in the question that I am aware of sizeToScene() method.

    – Sai Dandem
    Nov 28 '18 at 1:54











  • Anything beyond that will probably be way more complicated.

    – Sedrick
    Nov 28 '18 at 2:05






  • 1





    Thanks Sedrick. For time being I am relying on Jai's solution of keeping the sizeToScene() in the layoutChildren method of the root node. May be this is an expensive call. But atleast I dont need to worry about repeating the same line.

    – Sai Dandem
    Nov 28 '18 at 3:24
















0















I am checking for a feature to automatically resize the window based on the content. I am already aware of Window.sizeToScene() method. But this is so cumbersome that I need to take care in every place to call the sizeToScene(). For example, when I add/remove nodes, when I expanded the titlePanes, etc...



Can someone let me know if it is possible to automatically adjust the window based on the content. I am looking this feature for both normal and transparent windows.



Any hints/suggestion is highly appreciated.



Please find below a quick demo of what I am looking for. Everything works as expected if I consider calling sizeToScene(). I am targeting for a way to get the same behavior without calling sizeToScene for every scenario.



enter image description here



import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class TransparentWindowResizeDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
VBox root = new VBox();
root.setSpacing(15);
root.setAlignment(Pos.CENTER);

Scene sc = new Scene(root, 300, 300);
stage.setScene(sc);
stage.show();

Button showNormal = new Button("Show Normal Window");
showNormal.setOnAction(e -> showWindow(false));

Button showTransparent = new Button("Show Transparent Window");
showTransparent.setOnAction(e -> showWindow(true));

root.getChildren().addAll(showNormal, showTransparent);
}

private void showWindow(boolean isTransparent) {
Stage window = new Stage();
VBox root = new VBox();
root.setStyle("-fx-background-color: grey; -fx-border-width:2px;-fx-border-color:black;");
Scene scene = new Scene(root, Color.TRANSPARENT);
window.setScene(scene);

VBox layout = new VBox();
layout.setSpacing(5);
layout.setPadding(new Insets(5));

CheckBox sizeToScene = new CheckBox("sizeToScene");
sizeToScene.setSelected(true);

StackPane box = new StackPane();
box.setStyle("-fx-background-color:yellow; -fx-border-width:1px;-fx-border-color:black;");
box.setMinSize(200, 100);
box.setMaxSize(200, 100);

Button add = new Button("Add Pane");
Button remove = new Button("Remove Pane");
remove.setDisable(true);
add.setOnAction(e -> {
layout.getChildren().add(box);
add.setDisable(true);
remove.setDisable(false);
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});
remove.setOnAction(e -> {
layout.getChildren().remove(box);
add.setDisable(false);
remove.setDisable(true);
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});
HBox btnLayout = new HBox();
btnLayout.setSpacing(5);
btnLayout.getChildren().addAll(add, remove);

StackPane tpContent = new StackPane();
tpContent.setStyle("-fx-background-color:pink; -fx-border-width:1px;-fx-border-color:black;");
tpContent.setMinSize(200, 100);
tpContent.setMaxSize(200, 100);

TitledPane tp = new TitledPane("Titled Pane", tpContent);
tp.setAnimated(false);
tp.expandedProperty().addListener((obs, oldVal, newVal) -> {
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});

if (isTransparent) {
window.initStyle(StageStyle.TRANSPARENT);

StackPane close = new StackPane();
close.setMaxWidth(30);
close.setStyle("-fx-background-color:red;");
close.getChildren().add(new Label("X"));
close.setOnMouseClicked(e -> window.hide());

DoubleProperty x = new SimpleDoubleProperty();
DoubleProperty y = new SimpleDoubleProperty();
StackPane header = new StackPane();
header.setOnMousePressed(e -> {
x.set(e.getSceneX());
y.set(e.getSceneY());
});
header.setOnMouseDragged(e -> {
window.setX(e.getScreenX() - x.get());
window.setY(e.getScreenY() - y.get());
});
header.setStyle("-fx-border-width:0px 0px 2px 0px;-fx-border-color:black;");
header.setMinHeight(30);
header.setAlignment(Pos.CENTER_RIGHT);
Label lbl = new Label("I am draggable !!");
lbl.setStyle("-fx-text-fill:white;-fx-font-weight:bold;");
StackPane.setAlignment(lbl,Pos.CENTER_LEFT);
header.getChildren().addAll(lbl,close);
root.getChildren().add(header);
}

layout.getChildren().addAll(sizeToScene, btnLayout, tp);
root.getChildren().add(layout);
window.show();

}

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


Edit :: This is different from what I already found in other questions or with the link in the comment. The solution that is specified is what I am aware of and I already mentioned that in the question. I have a heavy screen where it contain many nodes in differnt hirerchy. I am checking for any generic solution to include in one place rather that calling from every node add/remove scenario.










share|improve this question

























  • Possible duplicate of JavaFX Stage/Scene automatic resize

    – Sedrick
    Nov 28 '18 at 0:46











  • @Sedrick, I already mentioned in the question that I am aware of sizeToScene() method.

    – Sai Dandem
    Nov 28 '18 at 1:54











  • Anything beyond that will probably be way more complicated.

    – Sedrick
    Nov 28 '18 at 2:05






  • 1





    Thanks Sedrick. For time being I am relying on Jai's solution of keeping the sizeToScene() in the layoutChildren method of the root node. May be this is an expensive call. But atleast I dont need to worry about repeating the same line.

    – Sai Dandem
    Nov 28 '18 at 3:24














0












0








0








I am checking for a feature to automatically resize the window based on the content. I am already aware of Window.sizeToScene() method. But this is so cumbersome that I need to take care in every place to call the sizeToScene(). For example, when I add/remove nodes, when I expanded the titlePanes, etc...



Can someone let me know if it is possible to automatically adjust the window based on the content. I am looking this feature for both normal and transparent windows.



Any hints/suggestion is highly appreciated.



Please find below a quick demo of what I am looking for. Everything works as expected if I consider calling sizeToScene(). I am targeting for a way to get the same behavior without calling sizeToScene for every scenario.



enter image description here



import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class TransparentWindowResizeDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
VBox root = new VBox();
root.setSpacing(15);
root.setAlignment(Pos.CENTER);

Scene sc = new Scene(root, 300, 300);
stage.setScene(sc);
stage.show();

Button showNormal = new Button("Show Normal Window");
showNormal.setOnAction(e -> showWindow(false));

Button showTransparent = new Button("Show Transparent Window");
showTransparent.setOnAction(e -> showWindow(true));

root.getChildren().addAll(showNormal, showTransparent);
}

private void showWindow(boolean isTransparent) {
Stage window = new Stage();
VBox root = new VBox();
root.setStyle("-fx-background-color: grey; -fx-border-width:2px;-fx-border-color:black;");
Scene scene = new Scene(root, Color.TRANSPARENT);
window.setScene(scene);

VBox layout = new VBox();
layout.setSpacing(5);
layout.setPadding(new Insets(5));

CheckBox sizeToScene = new CheckBox("sizeToScene");
sizeToScene.setSelected(true);

StackPane box = new StackPane();
box.setStyle("-fx-background-color:yellow; -fx-border-width:1px;-fx-border-color:black;");
box.setMinSize(200, 100);
box.setMaxSize(200, 100);

Button add = new Button("Add Pane");
Button remove = new Button("Remove Pane");
remove.setDisable(true);
add.setOnAction(e -> {
layout.getChildren().add(box);
add.setDisable(true);
remove.setDisable(false);
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});
remove.setOnAction(e -> {
layout.getChildren().remove(box);
add.setDisable(false);
remove.setDisable(true);
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});
HBox btnLayout = new HBox();
btnLayout.setSpacing(5);
btnLayout.getChildren().addAll(add, remove);

StackPane tpContent = new StackPane();
tpContent.setStyle("-fx-background-color:pink; -fx-border-width:1px;-fx-border-color:black;");
tpContent.setMinSize(200, 100);
tpContent.setMaxSize(200, 100);

TitledPane tp = new TitledPane("Titled Pane", tpContent);
tp.setAnimated(false);
tp.expandedProperty().addListener((obs, oldVal, newVal) -> {
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});

if (isTransparent) {
window.initStyle(StageStyle.TRANSPARENT);

StackPane close = new StackPane();
close.setMaxWidth(30);
close.setStyle("-fx-background-color:red;");
close.getChildren().add(new Label("X"));
close.setOnMouseClicked(e -> window.hide());

DoubleProperty x = new SimpleDoubleProperty();
DoubleProperty y = new SimpleDoubleProperty();
StackPane header = new StackPane();
header.setOnMousePressed(e -> {
x.set(e.getSceneX());
y.set(e.getSceneY());
});
header.setOnMouseDragged(e -> {
window.setX(e.getScreenX() - x.get());
window.setY(e.getScreenY() - y.get());
});
header.setStyle("-fx-border-width:0px 0px 2px 0px;-fx-border-color:black;");
header.setMinHeight(30);
header.setAlignment(Pos.CENTER_RIGHT);
Label lbl = new Label("I am draggable !!");
lbl.setStyle("-fx-text-fill:white;-fx-font-weight:bold;");
StackPane.setAlignment(lbl,Pos.CENTER_LEFT);
header.getChildren().addAll(lbl,close);
root.getChildren().add(header);
}

layout.getChildren().addAll(sizeToScene, btnLayout, tp);
root.getChildren().add(layout);
window.show();

}

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


Edit :: This is different from what I already found in other questions or with the link in the comment. The solution that is specified is what I am aware of and I already mentioned that in the question. I have a heavy screen where it contain many nodes in differnt hirerchy. I am checking for any generic solution to include in one place rather that calling from every node add/remove scenario.










share|improve this question
















I am checking for a feature to automatically resize the window based on the content. I am already aware of Window.sizeToScene() method. But this is so cumbersome that I need to take care in every place to call the sizeToScene(). For example, when I add/remove nodes, when I expanded the titlePanes, etc...



Can someone let me know if it is possible to automatically adjust the window based on the content. I am looking this feature for both normal and transparent windows.



Any hints/suggestion is highly appreciated.



Please find below a quick demo of what I am looking for. Everything works as expected if I consider calling sizeToScene(). I am targeting for a way to get the same behavior without calling sizeToScene for every scenario.



enter image description here



import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class TransparentWindowResizeDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
VBox root = new VBox();
root.setSpacing(15);
root.setAlignment(Pos.CENTER);

Scene sc = new Scene(root, 300, 300);
stage.setScene(sc);
stage.show();

Button showNormal = new Button("Show Normal Window");
showNormal.setOnAction(e -> showWindow(false));

Button showTransparent = new Button("Show Transparent Window");
showTransparent.setOnAction(e -> showWindow(true));

root.getChildren().addAll(showNormal, showTransparent);
}

private void showWindow(boolean isTransparent) {
Stage window = new Stage();
VBox root = new VBox();
root.setStyle("-fx-background-color: grey; -fx-border-width:2px;-fx-border-color:black;");
Scene scene = new Scene(root, Color.TRANSPARENT);
window.setScene(scene);

VBox layout = new VBox();
layout.setSpacing(5);
layout.setPadding(new Insets(5));

CheckBox sizeToScene = new CheckBox("sizeToScene");
sizeToScene.setSelected(true);

StackPane box = new StackPane();
box.setStyle("-fx-background-color:yellow; -fx-border-width:1px;-fx-border-color:black;");
box.setMinSize(200, 100);
box.setMaxSize(200, 100);

Button add = new Button("Add Pane");
Button remove = new Button("Remove Pane");
remove.setDisable(true);
add.setOnAction(e -> {
layout.getChildren().add(box);
add.setDisable(true);
remove.setDisable(false);
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});
remove.setOnAction(e -> {
layout.getChildren().remove(box);
add.setDisable(false);
remove.setDisable(true);
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});
HBox btnLayout = new HBox();
btnLayout.setSpacing(5);
btnLayout.getChildren().addAll(add, remove);

StackPane tpContent = new StackPane();
tpContent.setStyle("-fx-background-color:pink; -fx-border-width:1px;-fx-border-color:black;");
tpContent.setMinSize(200, 100);
tpContent.setMaxSize(200, 100);

TitledPane tp = new TitledPane("Titled Pane", tpContent);
tp.setAnimated(false);
tp.expandedProperty().addListener((obs, oldVal, newVal) -> {
if (sizeToScene.isSelected()) {
window.sizeToScene();
}
});

if (isTransparent) {
window.initStyle(StageStyle.TRANSPARENT);

StackPane close = new StackPane();
close.setMaxWidth(30);
close.setStyle("-fx-background-color:red;");
close.getChildren().add(new Label("X"));
close.setOnMouseClicked(e -> window.hide());

DoubleProperty x = new SimpleDoubleProperty();
DoubleProperty y = new SimpleDoubleProperty();
StackPane header = new StackPane();
header.setOnMousePressed(e -> {
x.set(e.getSceneX());
y.set(e.getSceneY());
});
header.setOnMouseDragged(e -> {
window.setX(e.getScreenX() - x.get());
window.setY(e.getScreenY() - y.get());
});
header.setStyle("-fx-border-width:0px 0px 2px 0px;-fx-border-color:black;");
header.setMinHeight(30);
header.setAlignment(Pos.CENTER_RIGHT);
Label lbl = new Label("I am draggable !!");
lbl.setStyle("-fx-text-fill:white;-fx-font-weight:bold;");
StackPane.setAlignment(lbl,Pos.CENTER_LEFT);
header.getChildren().addAll(lbl,close);
root.getChildren().add(header);
}

layout.getChildren().addAll(sizeToScene, btnLayout, tp);
root.getChildren().add(layout);
window.show();

}

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


Edit :: This is different from what I already found in other questions or with the link in the comment. The solution that is specified is what I am aware of and I already mentioned that in the question. I have a heavy screen where it contain many nodes in differnt hirerchy. I am checking for any generic solution to include in one place rather that calling from every node add/remove scenario.







javafx javafx-8






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 1:52







Sai Dandem

















asked Nov 28 '18 at 0:23









Sai DandemSai Dandem

1,06849




1,06849













  • Possible duplicate of JavaFX Stage/Scene automatic resize

    – Sedrick
    Nov 28 '18 at 0:46











  • @Sedrick, I already mentioned in the question that I am aware of sizeToScene() method.

    – Sai Dandem
    Nov 28 '18 at 1:54











  • Anything beyond that will probably be way more complicated.

    – Sedrick
    Nov 28 '18 at 2:05






  • 1





    Thanks Sedrick. For time being I am relying on Jai's solution of keeping the sizeToScene() in the layoutChildren method of the root node. May be this is an expensive call. But atleast I dont need to worry about repeating the same line.

    – Sai Dandem
    Nov 28 '18 at 3:24



















  • Possible duplicate of JavaFX Stage/Scene automatic resize

    – Sedrick
    Nov 28 '18 at 0:46











  • @Sedrick, I already mentioned in the question that I am aware of sizeToScene() method.

    – Sai Dandem
    Nov 28 '18 at 1:54











  • Anything beyond that will probably be way more complicated.

    – Sedrick
    Nov 28 '18 at 2:05






  • 1





    Thanks Sedrick. For time being I am relying on Jai's solution of keeping the sizeToScene() in the layoutChildren method of the root node. May be this is an expensive call. But atleast I dont need to worry about repeating the same line.

    – Sai Dandem
    Nov 28 '18 at 3:24

















Possible duplicate of JavaFX Stage/Scene automatic resize

– Sedrick
Nov 28 '18 at 0:46





Possible duplicate of JavaFX Stage/Scene automatic resize

– Sedrick
Nov 28 '18 at 0:46













@Sedrick, I already mentioned in the question that I am aware of sizeToScene() method.

– Sai Dandem
Nov 28 '18 at 1:54





@Sedrick, I already mentioned in the question that I am aware of sizeToScene() method.

– Sai Dandem
Nov 28 '18 at 1:54













Anything beyond that will probably be way more complicated.

– Sedrick
Nov 28 '18 at 2:05





Anything beyond that will probably be way more complicated.

– Sedrick
Nov 28 '18 at 2:05




1




1





Thanks Sedrick. For time being I am relying on Jai's solution of keeping the sizeToScene() in the layoutChildren method of the root node. May be this is an expensive call. But atleast I dont need to worry about repeating the same line.

– Sai Dandem
Nov 28 '18 at 3:24





Thanks Sedrick. For time being I am relying on Jai's solution of keeping the sizeToScene() in the layoutChildren method of the root node. May be this is an expensive call. But atleast I dont need to worry about repeating the same line.

– Sai Dandem
Nov 28 '18 at 3:24












1 Answer
1






active

oldest

votes


















2














I would say this is not a graceful solution (it's more like a hack), but at least it has worked using your example:



VBox root = new VBox() {
private boolean needsResize = false;

@Override
protected void layoutChildren()
{
super.layoutChildren();

if (!needsResize) {
needsResize = true;

Platform.runLater(() -> {
if (needsResize) {
window.sizeToScene();
needsResize = false;
}
});
}
}
};


Of course, you should add in the sizeToScene.isSelected() part, and you could also make this an actual subclass.






share|improve this answer
























  • Jai, Thanks for providing an approach :). I didnt think of layoutChildren() method. Atleast I can rely on this. This addresses my actual question of keeping the required logic in one place.

    – Sai Dandem
    Nov 28 '18 at 3:21













  • @SaiDandem I'm still not sure if this has any side-effects though. If you are using this, I definitely recommend doing comprehension tests to make sure it's not giving you any weird problem.

    – Jai
    Nov 28 '18 at 3:25











  • As far as I am aware of, sizeToScene() is a bit expensive call. There is considerable difference in performance between with/without calling sizeToScene. But I can tradeoff that for my requirement.

    – Sai Dandem
    Nov 28 '18 at 3:27













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%2f53510283%2fautomatically-resizing-the-window-based-on-dynamic-content%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









2














I would say this is not a graceful solution (it's more like a hack), but at least it has worked using your example:



VBox root = new VBox() {
private boolean needsResize = false;

@Override
protected void layoutChildren()
{
super.layoutChildren();

if (!needsResize) {
needsResize = true;

Platform.runLater(() -> {
if (needsResize) {
window.sizeToScene();
needsResize = false;
}
});
}
}
};


Of course, you should add in the sizeToScene.isSelected() part, and you could also make this an actual subclass.






share|improve this answer
























  • Jai, Thanks for providing an approach :). I didnt think of layoutChildren() method. Atleast I can rely on this. This addresses my actual question of keeping the required logic in one place.

    – Sai Dandem
    Nov 28 '18 at 3:21













  • @SaiDandem I'm still not sure if this has any side-effects though. If you are using this, I definitely recommend doing comprehension tests to make sure it's not giving you any weird problem.

    – Jai
    Nov 28 '18 at 3:25











  • As far as I am aware of, sizeToScene() is a bit expensive call. There is considerable difference in performance between with/without calling sizeToScene. But I can tradeoff that for my requirement.

    – Sai Dandem
    Nov 28 '18 at 3:27


















2














I would say this is not a graceful solution (it's more like a hack), but at least it has worked using your example:



VBox root = new VBox() {
private boolean needsResize = false;

@Override
protected void layoutChildren()
{
super.layoutChildren();

if (!needsResize) {
needsResize = true;

Platform.runLater(() -> {
if (needsResize) {
window.sizeToScene();
needsResize = false;
}
});
}
}
};


Of course, you should add in the sizeToScene.isSelected() part, and you could also make this an actual subclass.






share|improve this answer
























  • Jai, Thanks for providing an approach :). I didnt think of layoutChildren() method. Atleast I can rely on this. This addresses my actual question of keeping the required logic in one place.

    – Sai Dandem
    Nov 28 '18 at 3:21













  • @SaiDandem I'm still not sure if this has any side-effects though. If you are using this, I definitely recommend doing comprehension tests to make sure it's not giving you any weird problem.

    – Jai
    Nov 28 '18 at 3:25











  • As far as I am aware of, sizeToScene() is a bit expensive call. There is considerable difference in performance between with/without calling sizeToScene. But I can tradeoff that for my requirement.

    – Sai Dandem
    Nov 28 '18 at 3:27
















2












2








2







I would say this is not a graceful solution (it's more like a hack), but at least it has worked using your example:



VBox root = new VBox() {
private boolean needsResize = false;

@Override
protected void layoutChildren()
{
super.layoutChildren();

if (!needsResize) {
needsResize = true;

Platform.runLater(() -> {
if (needsResize) {
window.sizeToScene();
needsResize = false;
}
});
}
}
};


Of course, you should add in the sizeToScene.isSelected() part, and you could also make this an actual subclass.






share|improve this answer













I would say this is not a graceful solution (it's more like a hack), but at least it has worked using your example:



VBox root = new VBox() {
private boolean needsResize = false;

@Override
protected void layoutChildren()
{
super.layoutChildren();

if (!needsResize) {
needsResize = true;

Platform.runLater(() -> {
if (needsResize) {
window.sizeToScene();
needsResize = false;
}
});
}
}
};


Of course, you should add in the sizeToScene.isSelected() part, and you could also make this an actual subclass.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 2:47









JaiJai

5,85411233




5,85411233













  • Jai, Thanks for providing an approach :). I didnt think of layoutChildren() method. Atleast I can rely on this. This addresses my actual question of keeping the required logic in one place.

    – Sai Dandem
    Nov 28 '18 at 3:21













  • @SaiDandem I'm still not sure if this has any side-effects though. If you are using this, I definitely recommend doing comprehension tests to make sure it's not giving you any weird problem.

    – Jai
    Nov 28 '18 at 3:25











  • As far as I am aware of, sizeToScene() is a bit expensive call. There is considerable difference in performance between with/without calling sizeToScene. But I can tradeoff that for my requirement.

    – Sai Dandem
    Nov 28 '18 at 3:27





















  • Jai, Thanks for providing an approach :). I didnt think of layoutChildren() method. Atleast I can rely on this. This addresses my actual question of keeping the required logic in one place.

    – Sai Dandem
    Nov 28 '18 at 3:21













  • @SaiDandem I'm still not sure if this has any side-effects though. If you are using this, I definitely recommend doing comprehension tests to make sure it's not giving you any weird problem.

    – Jai
    Nov 28 '18 at 3:25











  • As far as I am aware of, sizeToScene() is a bit expensive call. There is considerable difference in performance between with/without calling sizeToScene. But I can tradeoff that for my requirement.

    – Sai Dandem
    Nov 28 '18 at 3:27



















Jai, Thanks for providing an approach :). I didnt think of layoutChildren() method. Atleast I can rely on this. This addresses my actual question of keeping the required logic in one place.

– Sai Dandem
Nov 28 '18 at 3:21







Jai, Thanks for providing an approach :). I didnt think of layoutChildren() method. Atleast I can rely on this. This addresses my actual question of keeping the required logic in one place.

– Sai Dandem
Nov 28 '18 at 3:21















@SaiDandem I'm still not sure if this has any side-effects though. If you are using this, I definitely recommend doing comprehension tests to make sure it's not giving you any weird problem.

– Jai
Nov 28 '18 at 3:25





@SaiDandem I'm still not sure if this has any side-effects though. If you are using this, I definitely recommend doing comprehension tests to make sure it's not giving you any weird problem.

– Jai
Nov 28 '18 at 3:25













As far as I am aware of, sizeToScene() is a bit expensive call. There is considerable difference in performance between with/without calling sizeToScene. But I can tradeoff that for my requirement.

– Sai Dandem
Nov 28 '18 at 3:27







As far as I am aware of, sizeToScene() is a bit expensive call. There is considerable difference in performance between with/without calling sizeToScene. But I can tradeoff that for my requirement.

– Sai Dandem
Nov 28 '18 at 3:27






















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%2f53510283%2fautomatically-resizing-the-window-based-on-dynamic-content%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







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)