简体   繁体   中英

How to make my nodes auto adjusting its contents during the run time in JavaFx/FXML?

The nodes get an automatic resize at the first load of FXML, but it stays the same when I change the texts in label during the run time.

Minimal, Complete, and Verifiable example

Main Class:

public class Main extends Application {
    private Stage stage;
    private StackPane stackPane;
    @Override
    public void start(Stage primaryStage) throws Exception{
        stage = primaryStage;
        FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
        Parent root = (Parent) loader.load();
        stackPane = new StackPane(root);
        Scene scene = new Scene(stackPane);
        scene.setFill(Color.WHITE);
        stage.setScene(scene);
        stage.show();
    }


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

Controller Class:

public class Controller implements Initializable{
    @FXML private Label label1;
    @FXML private Button changeLabel;
    private StringProperty labelString = new SimpleStringProperty();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        labelString.setValue("aasdasd sad asdasd asdasda");
        label1.textProperty().bind(labelString);

    }
    @FXML
    public void clicked(MouseEvent e){
        labelString.setValue("asdsadasd asdasdasd sfdgsfwoef fgtrhfgbdrgdf dfgdfivbjkfd gdfgidfjvdf gdfgjldkvbdf gjdilgjdfv dfgojdflkgdf ");
    }
}

sample.fxml:

<GridPane fx:controller="sample.Controller"
      xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10" maxHeight="Infinity">
<children>
    <VBox fx:id="body"
          alignment="CENTER"
          maxHeight="Infinity"
          GridPane.vgrow="SOMETIMES"
          prefWidth="300"
          GridPane.columnIndex="0"
          GridPane.rowIndex="1" spacing="5">

    <Label alignment="CENTER" VBox.vgrow="ALWAYS" wrapText="true" textAlignment="CENTER" fx:id="label1" maxWidth="268"/>
    <Button fx:id="changeLabel" text="Change" minWidth="50" onMouseClicked="#clicked" maxWidth="Infinity" />
    </VBox>
</children>
</GridPane>

Expectation: When I press 'Change' button, Everything should resize to just give enough space for text to be shown.

Problem: When I press 'Change' button, UI remains same not showing the full text.

The auto-adjustment depends on the layout pane that you are using. I suggest you take a look at some of the documentation, eg Using Built-in Layout Panes .

As for your case, you want to resize the stage. Nobody could possibly guess what exactly you want. Do you want it to change the width or the height in order to meet your requirements?

However, here's an example solution for your case.

Add this to the controller:

public StringProperty getStringProperty() {
    return labelString;
}

and this to the main class:

Controller controller = loader.getController();
StringProperty stringproperty = controller.getStringProperty();
stringproperty.addListener( (ChangeListener<String>) (observable, oldValue, newValue) -> {
    stage.setHeight(400); // TODO: calculate width/height depending on what your requirements are
});

Alternatively you can hand over the stage to the controller. As I said that's just an example of many possible solutions. You need to be more specific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM