Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Getting Size of Components

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Getting Size of Components

    Ok, I need to be able to get the current size of a component. I have attempted to use the component's getWidth() method, getHeight() method, getSize() method, and the getPreferredSize() method but they all return 0 or dimensions that have a height and width of 0 (depending on the method).

    My components have not been given a specific size by me, they should be getting their size based on what they contain or specifications on construction. For example, the size of a JLabel is the length of the String given to it. The size of a JTextField is the number of columns given to. The size of a JButton is the length of the String given to it. And those sorts of things.

    I believe that because of this setup, I am getting 0 because I have not explicitly set a size or a preferred size. And for what I am doing, I shouldnt be sending it a size or a preferred size anyway.

    As well as getting the sizes of normal components, I want to be able to get the size of a JPanel when I send it one. I havent managed to get that to work either.

    Is anyone able to send me in the correct direction?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,334
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Getting Size of Components

    When are you trying to retrieve the size? Is this before they are visible or packed within a JFrame? These methods return 0 prior to this:
    	public static void main(String[] args){
     
    		JPanel panel = new JPanel();
    		JLabel lab = new JLabel("Label");
    		panel.add(lab);
    		System.out.println(lab.getWidth());//
    		JFrame frame = new JFrame();
    		frame.getContentPane().add(panel);
    		frame.pack();
    		System.out.println(lab.getWidth());
    		frame.setVisible(true);
     
    	}

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Getting Size of Components

    This will be before packing because I am attempting to create a centerComponent(Component,int) method in my custom layout. It will need to subtract the width of the Component from the width of the JPanel and divide by 2 to find out where it needs to be placed for an X coordinate.

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    495
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Getting Size of Components

    Looks to me that you don't need to get the size, you need to get the preferredSize.

    db

Similar Threads

  1. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  2. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM
  3. How do you layout these components centered
    By robertbob in forum AWT / Java Swing
    Replies: 2
    Last Post: May 24th, 2010, 11:52 PM
  4. Components that interact with eachother
    By Flamespewer in forum AWT / Java Swing
    Replies: 2
    Last Post: February 25th, 2010, 08:27 PM
  5. How can i make the components resizable?
    By ces_31 in forum AWT / Java Swing
    Replies: 4
    Last Post: October 1st, 2009, 10:46 AM