Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Sutured

Thread: Get the dimensions of a Node

  1. #1
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Get the dimensions of a Node

    Hello all,

    I want to take the dimensions of a node.

    I created a mini program. Although it works, it returns 0 and 0 for the width and height respectively.

    Java FX Code:
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class Frame extends Application {
    
    	@Override
    	public void start(final Stage primaryStage) throws Exception {
    
    		primaryStage.setTitle("Test");
    		primaryStage.setResizable(false);
    		primaryStage.setScene(new Scene(new MyPanel(),200, 100));
    		primaryStage.centerOnScreen();
    		primaryStage.show();
    
    	}
    
    	public static void main(String[] args) {
    		launch(args);
    	}
    
    }
    Java FX Code:
    import javafx.scene.control.Label;
    import javafx.scene.layout.Pane;
    import javafx.scene.text.Font;
    
    
    public class MyPanel extends Pane {
    	
    	private Label label = new Label("Test");
    	
    	public MyPanel() {
    		
    		this.getChildren().add(label);
    		label.setFont(new Font(25));
    		label.relocate(10, 10);
    		
    		System.out.println(label.getWidth());
    		System.out.println(label.getHeight());
    		
    	}
    
    }

    I have tried several things, I looked it up at FX API and I google it but seems like I can't find the answer.

    Why is that? What can I do to get the dimensions?


    Thank you

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Get the dimensions of a Node

    It's possible (similar to Swing) that those values haven't been calculated at that point.
    I'm not sure at what point FX does its sizing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Sutured is offline Member
    Join Date
    Mar 2012
    Posts
    30
    Rep Power
    0

    Default Re: Get the dimensions of a Node

    Hello Tolls and thank you,

    Finally I figured that out. It calculates the dimensions after the primaryStage.show() method completes.



    Java FX Code:
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class Frame extends Application {
    	
    	private MyPanel myPanel = new MyPanel();
    
    	@Override
    	public void start(final Stage primaryStage) throws Exception {
    
    		primaryStage.setTitle("Test");
    		primaryStage.setResizable(false);
    		primaryStage.setScene(new Scene(myPanel, 200, 100));
    		myPanel.print();
    		primaryStage.centerOnScreen();
    		primaryStage.show();
    		
    		myPanel.print();
    
    	}
    
    	public static void main(String[] args) {
    		launch(args);
    	}
    
    }

    Java FX Code:
    import javafx.scene.control.Label;
    import javafx.scene.layout.Pane;
    import javafx.scene.text.Font;
    
    
    public class MyPanel extends Pane {
    	
    	private Label label = new Label("Test");
    	
    	public MyPanel() {
    		
    		this.getChildren().add(label);
    		
    		label.setFont(new Font(25));
    		label.relocate(10, 10);
    		
    		System.out.println(label.getWidth());
    		System.out.println(label.getHeight());
    		System.out.println();
    		
    	}
    	
    	public void print() {
    		
    		System.out.println();
    		System.out.println(label.getWidth());
    		System.out.println(label.getHeight());
    	}
    
    }
    The console outcome is this:

    Java FX Code:
    0.0
    0.0
    
    
    0.0
    0.0
    
    46.0
    33.251953125
    The first group of dimensions is being printed in the MyPanel constructor, being 0.

    The second one comes from line 15, which I call the print() method. Still 0.

    After the .show() I call again the print and it gives me the dimensions.
    DarrylBurke likes this.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default Re: Get the dimensions of a Node

    Thank you for sharing this information.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Keeping the dimensions of a JPanel.
    By willemien in forum AWT / Swing
    Replies: 13
    Last Post: 03-06-2013, 09:17 AM
  2. Replies: 1
    Last Post: 08-12-2012, 03:15 AM
  3. Image Dimensions in JFrame
    By BrooksyFC in forum Java 2D
    Replies: 5
    Last Post: 03-29-2012, 03:25 PM
  4. Replies: 2
    Last Post: 03-13-2011, 04:29 AM
  5. how to set full screen dimensions
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 06:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •