package javaapplication35; import javafx.collections.ListChangeListener.Change; import javafx.event.EventHandler; import javafx.beans.*; import javafx.beans.property.*; import javafx.application.Application; import javafx.beans.property.BooleanProperty; import javafx.beans.property.ObjectProperty; import javafx.collections.FXCollections; import javafx.collections.ListChangeListener; import javafx.scene.*; import javafx.stage.*; import javafx.scene.shape.*; import javafx.scene.paint.Color; import javafx.scene.input.*; import javafx.scene.image.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.collections.ObservableList; public class AutoSize_Test7 extends Application { private Scene scene; private ObjectProperty TEXT = new SimpleObjectProperty(); private Button node1; private Label node3; private boolean toggleText = false; private Label label; public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) throws Exception { scene = new Scene(new Group(), 300, 300); int bizzareValue = 220; TEXT.setValue("Resizable"); final BooleanProperty MANAGED = new SimpleBooleanProperty(true); node1 = new Button(TEXT.getValue()); TEXT.addListener(new InvalidationListener() { public void invalidated(Observable vm) { node1.setText(TEXT.getValue()); node3.setText(TEXT.getValue()); } }); node1.setManaged(MANAGED.getValue()); MANAGED.addListener(new InvalidationListener() { public void invalidated(Observable vm) { node1.setManaged(MANAGED.getValue()); node3.setManaged(MANAGED.getValue()); label.setText((MANAGED.getValue()) ? ("Button, Label are managed\nImageView always unmanaged") : ("Button, Label are not managed\nImageView always unmanaged")); } }); ImageView node2 = new ImageView(); Image image = new Image(AutoSize_Test7.class.getResourceAsStream("DUKE.png"), 50.0F, 50.0F, false, false); node2.setImage(image); node2.setX(bizzareValue); node2.setY(bizzareValue); node3 = new Label(TEXT.getValue()); node3.setManaged(MANAGED.getValue()); MyCustomNode customNode = new MyCustomNode(); customNode.content.clear(); customNode.content.addAll(node1, node2, node3); Rectangle bgRect = new Rectangle(0.0F, 0.0F, 300, 300); bgRect.setFill(Color.TRANSPARENT); bgRect.setOpacity(0.3F); bgRect.setFocusTraversable(true); bgRect.setOnKeyPressed(new EventHandler() { @Override public void handle(KeyEvent ke) { if (ke.getCode().equals(KeyCode.RIGHT)) { toggleText = !toggleText; if (toggleText) { TEXT.setValue("This is a Very Long Text"); } else { TEXT.setValue("Resizable"); } } if (ke.getCode().equals(KeyCode.LEFT)) { MANAGED.setValue(!MANAGED.getValue()); } } }); label = new Label((MANAGED.getValue()) ? ("Button, Label are managed\nImageView always unmanaged") : ("Button, Label are not managed\nImageView always unmanaged")); label.setTranslateY(190.0F); ((Group) scene.getRoot()).getChildren().clear(); ((Group) scene.getRoot()).getChildren().addAll(bgRect, customNode, label); stage.setScene(scene); bgRect.requestFocus(); stage.show(); } static class MyCustomNode extends Parent { private ObservableList content = FXCollections.observableArrayList(); HBox hbox = new HBox(); MyCustomNode() { content.addListener(new ListChangeListener() { public void onChanged(Change change) { hbox.getChildren().clear(); hbox.getChildren().addAll(content); } }); hbox.getChildren().clear(); hbox.getChildren().addAll(content); getChildren().add(hbox); } } }