3 Replies Latest reply on Dec 13, 2012 7:54 PM by ellias2007

    Node.getSize()  ??

      Hi All,

      My Q is very simple ...

      How to get the size (width-height) to a node ???

      i expect that there are : MyNode.getWidth() and MyNode.getHeight() ..
      But they does NOT exist...nor any equivalent methods ...

      It is really ubnormal ???

      Thanks for any help...
        • 1. Re: Node.getSize()  ??
          And what about boundsInLocal or boundsInParent?
          1 person found this helpful
          • 2. Re: Node.getSize()  ??
            Things that extend region (e.g. layouts and controls) have width and height properties, so you can just do:
            Nodes have different kinds of bounds =>

            A simple program to illustrate what these do:

            In my words:
            - Layout Bounds: The boundary of the shape.
            - Bounds in Local: The boundary of the shape and effect.
            - Bounds in Parent: The boundary of the shape, effect and transforms.

            Once you have the appropriate bounds, you can query it's width, height and depth.

            For example:
            Note that if the node contains something resizable or css styled, you won't really get a correct report for the bounds until it has been displayed on an active scene and a pulse has been run on it. So, for example, the value reported for the bounds immediately after you create the node may differ from the value reported after stage.show(scene) has been called. To always be notified of what the height or width of a node is, add a listener to the appropriate bounds property, for example:
            final DoubleProperty nodeWidth = new SimpleDoubleProperty(node.getWidth());
            node.boundsInParentProperty().addListener(new ChangeListener<Bounds>() {
              @Override public void changed(ObservableValue<? extends Bounds> ov, Bounds oldBounds, Bounds newBounds) {
            Controls and layout panes have a convenience property for this already, so you can do things like:
            Then there are things which calculate minimum, preferred and maximum sizes for nodes based on a given width or height. For constant sized nodes, these will always return the same value, for resizable nodes, they can return different values. These should only really be used when within a subclassed layoutChildren call you are creating your own composite nodes (i.e. not that often, and never if you are just starting to write JavaFX programs).
            node.minWidth(height)  // minimum width for a given height.
            Finally resizable nodes which extend Region like controls and layout panes have the following properties, which you will likely end up using quite a bit:
            For example region.getMaxWidth will either return
            - Region.USE_PREF_SIZE => maximum width will be the same as the preferred width.
            - Region.USE_COMPUTED_SIZE => maximum width will be whatever the region's implementation defaults it to.
            - Some absolute value => the maximum width will be whatever a caller has chosen via region.setMaxWidth(value) call.
            - Double.MAX_VALUE => the maximum width unlimited.

            A good layout strategy is not to worry about sizes much at all, instead use resizable controls inside layout managers and set a preferred size on the resizable controls only when you need to.
            • 3. Re: Node.getSize()  ??
              Many Many Thanks ...

              Edited by: ellias2007 on Dec 13, 2012 11:54 AM