Results 1 to 9 of 9
  1. #1
    mkj
    mkj is offline Member
    Join Date
    Jan 2012
    Posts
    15
    Rep Power
    0

    Default Jpanel.getHeight() is returning zero

    hi,
    i want to place a rectangle in a jpanel at the bottom left corner of the panel. i have thought of using the getheight() method and then using the object.setbounds(getWidth(), getHeight(),x,y) to place the object at the bottom left corner
    but the problem i am having is that both getWidth(), getHeight() are returning zero
    can any body help me

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Jpanel.getHeight() is returning zero

    Could you post a SSCCE?

  3. #3
    mkj
    mkj is offline Member
    Join Date
    Jan 2012
    Posts
    15
    Rep Power
    0

    Default Re: Jpanel.getHeight() is returning zero

    dear pbrockway2
    in my program i am using a syntax in the same order as the code i am posting here

    import my.graphics.MyRect;


    import java.awt.*;
    import java.awt.event.*;
    import java.awt.GridLayout;

    import javax.swing.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.Graphics;
    import java.awt.Rectangle;

    public class Xindepro{


    public static void main (String[] args)
    {
    JFrame frame1 = new JFrame("x indepedence of projectile motion");
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    frame1.setSize(900,900);

    JPanel Panel2 = new JPanel();
    Panel2.setBackground(Color.white);
    frame1.getContentPane().add(Panel2,BorderLayout.CE NTER);
    Panel2.setPreferredSize(new Dimension(600,600));
    int x = Panel2.getHeight();
    int y = Panel2.getWidth();

    Panel2.setLayout(null);
    MyRect car = new MyRect(0,0,30,50);
    car.setBounds(y,x,50,50);
    }
    frame1.setVisible(true);
    frame1.pack();
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Jpanel.getHeight() is returning zero

    The panel has its default size of [0, 0] until the frame is realized either by calling pack() or setVisible(true).

    Since you are adding the panel to the frame at BorderLayout.CENTER and then calling pack(0, the panel will be displayed at its preferredSize -- which you have set in code. So why do you feel the need to query the panel's width and height?

    Incidentally, setting the bounds of car to (y, x, 50, 50) will place it outside the visible area of the panel -- so it won't be seen. And those are singularly badly named variables -- x for height, which is in the direction of the y-axis, and vice versa. What prevented you from naming the variables width and height?

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

  5. #5
    mkj
    mkj is offline Member
    Join Date
    Jan 2012
    Posts
    15
    Rep Power
    0

    Default Re: Jpanel.getHeight() is returning zero

    Quote Originally Posted by DarrylBurke View Post
    The panel has its default size of [0, 0] until the frame is realized either by calling pack() or setVisible(true).

    Since you are adding the panel to the frame at BorderLayout.CENTER and then calling pack(0, the panel will be displayed at its preferredSize -- which you have set in code. So why do you feel the need to query the panel's width and height?
    from reading in the tutorial, i have understood that the setPreferredsize is not a fixed and it may change depending on layout manager that i am using. i was afraid if i set the bounds of the cars to the values declared in the preferredsize(600,600) that the car may not appear if the preferredsize values were decreased by the system
    Quote Originally Posted by DarrylBurke View Post
    Incidentally, setting the bounds of car to (y, x, 50, 50) will place it outside the visible area of the panel -- so it won't be seen.
    sorry i didnt get why you have considered that the car will not be seen, the car is showing but it is showing at the position (0,0) of Panel2 instead of showing at the position (y,x) this is my problem that i want to solve
    Quote Originally Posted by DarrylBurke View Post
    And those are singularly badly named variables -- x for height, which is in the direction of the y-axis, and vice versa. What prevented you from naming the variables width and height?
    you are right but i was just trying to test if i was able to place the car in the correct position or not

    db[/QUOTE]

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Jpanel.getHeight() is returning zero

    why you have considered that the car will not be seen, the car is showing but it is showing at the position (0,0) of Panel2 instead of showing at the position (y,x)
    And where is the position (y,x)?
    Java Code:
      0
    0 +----------------------+
      |                      |
      |                      |
      |                      |
      |                      |
      |        PANEL         |
      |                      |
      |                      |
      |                      |
      |                      |x
      +----------------------+-------+
                           y |       |
                             |  CAR  |
                             |       |
                             +-------+
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    mkj
    mkj is offline Member
    Join Date
    Jan 2012
    Posts
    15
    Rep Power
    0

    Default Re: Jpanel.getHeight() is returning zero

    Theoretically what you have drawn should be happening if we consider that the car is located at a position (y,x) but as i told you before i want to locate the car at a position
    (y,x) = (0, getheight()- height of the car). the problem is that i am always getting the car at the (0,0) position cuz the getheight() is returning a zero

    to simplifiy the problem what should i do if i want the getHeight() to return the actual height of the panel. In the code i have presented above where should i call the getHeight() method.
    thanks
    0
    0+---------------------------+
    | actual | |
    | position | |
    |--------- |
    | |
    | |
    | |
    | |
    |-------- |
    | desired| |
    | position| |
    +----------------------------+
    Last edited by mkj; 02-14-2012 at 09:24 AM. Reason: including a schema

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Jpanel.getHeight() is returning zero

    i want to locate the car at a position
    (y,x) = (0, getheight()- height of the car)
    So waste people's time by posting misleading code.
    Java Code:
    int x = Panel2.getHeight();
     int y = Panel2.getWidth();
    
     Panel2.setLayout(null);
     MyRect car = new MyRect(0,0,30,50);
     car.setBounds(y,x,50,50);
    to simplifiy the problem what should i do if i want the getHeight() to return the actual height of the panel.
    Is that a question? A question should end with a question mark. Also, a sentence should start with an uppercase letter. Please go through this page.

    I already told you:
    The panel has its default size of [0, 0] until the frame is realized either by calling pack() or setVisible(true).
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    mkj
    mkj is offline Member
    Join Date
    Jan 2012
    Posts
    15
    Rep Power
    0

    Default Re: Jpanel.getHeight() is returning zero

    Thanks for the grammar page.
    If i was wasting your time you can simply ignore my question, i don't think you are forced to answer my questions
    I would like to remind you that i am new to java and i am not getting where should i put the frame1.pack() in order to have a getheight() different of zero.
    regards.

Similar Threads

  1. Replies: 4
    Last Post: 02-13-2012, 10:54 PM
  2. Change JPanel text of Parent JPanel from JDialog
    By bikashlama in forum AWT / Swing
    Replies: 7
    Last Post: 12-09-2011, 03:47 AM
  3. getWidth(),GetHeight() using POI
    By Zeus in forum New To Java
    Replies: 0
    Last Post: 08-02-2010, 12:28 PM
  4. getWidth && getHeight
    By courteous in forum New To Java
    Replies: 1
    Last Post: 01-18-2009, 10:39 PM
  5. getHeight() problem
    By sapata in forum AWT / Swing
    Replies: 1
    Last Post: 05-18-2008, 04:16 AM

Posting Permissions

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