0

in Controller implement Initializable the initialize method I want get box LayoutBounds,but why layoutBounds all property is zero?

package com.erayt.studio.process.component.aggregate.controller;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.VBox;

import java.net.URL;
import java.util.ResourceBundle;

public class TestController implements Initializable {
    @FXML
    private VBox box;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        System.out.println(box.getLayoutBounds());
    }
}

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>


<VBox fx:id="box" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.erayt.studio.process.component.aggregate.controller.TestController">
   <children>
      <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab text="Untitled Tab 1">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </Tab>
          <Tab text="Untitled Tab 2">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </Tab>
        </tabs>
      </TabPane>
   </children>
</VBox>

Lson
  • 147
  • 8
  • 2
    it has no size because it's not yet added to the scenegraph - as an aside: don't hard-code sizing hints, ever. – kleopatra Nov 22 '21 at 10:26
  • @kleopatra what mean don't hard-code sizing hints? i want get box size to compute something,how can i get real size? – Lson Nov 22 '21 at 10:56
  • 1
    later, after it is added to the scenegraph. sizing hints == min/pref/max – kleopatra Nov 22 '21 at 12:05
  • *"I want to get the box size to compute something"*. This is probably the wrong approach to whatever it is you're trying to do. (I.e. this is likely an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem).) You should probably avoid this entirely by letting the appropriate layout manager do this work for you. If none of those provide the functionality you want, you should write a custom layout by subclassing `Pane` and do the layout computation in the `layoutChildren` method. – James_D Nov 22 '21 at 14:38
  • If absolutely necessary, you can [generate a layout pass](https://stackoverflow.com/questions/26152642/get-the-height-of-a-node-in-javafx-generate-a-layout-pass), but otherwise you should follow the advice from prior comments. – jewelsea Nov 22 '21 at 22:52

0 Answers0