3 Replies Latest reply: Dec 13, 2012 1:54 PM by ellias2007 RSS

    Node.getSize()  ??

    ellias2007
      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()  ??
          MiPa
          And what about boundsInLocal or boundsInParent?
          • 2. Re: Node.getSize()  ??
            jsmith
            Things that extend region (e.g. layouts and controls) have width and height properties, so you can just do:
            label.getWidth();
            Nodes have different kinds of bounds =>
            http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#layoutBoundsProperty%28%29
            http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#boundsInLocalProperty%28%29
            http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#boundsInParentProperty%28%29

            A simple program to illustrate what these do:
            https://gist.github.com/1441960

            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:
            node.getBoundsInParent().getWidth()
            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) {
                nodeWidth.set(newBounds.getWidth());
              }
            });
            Controls and layout panes have a convenience property for this already, so you can do things like:
            label.widthProperty().bind(scene.widthProperty());
            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.
            node.prefWidth(height)
            node.maxWidth(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:
            region.minWidthProperty()
            region.prefWidthProperty()
            region.maxWidthProperty()  
            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()  ??
              ellias2007
              Many Many Thanks ...

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