Results 1 to 3 of 3
  1. #1
    NoSwing is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default Please explain to where container dimensions come from

    Hi,

    Well, I'm finally painting my JPEGs to the screen. I've got a question though about where the containers dimensions come from. I was using an example from a previous post to this group actually. I've extened JPanel with my own panel and the constructor looks like this:

    public PhotoViewer(BufferedImage bi) {
    mBuffImg = bi;

    mPanelSize = new Dimension(bi.getWidth(), bi.getHeight());
    }

    My PhotoViewer panel had dimensions of 0x0 until I overrode the getWidth() and getHeight() methods. What seems odd to me is why this would be because that mPanelSize member field is being returned by my overridden getPreferredSize() method in this container. I thought that perhaps I should call the setPreferredSize() method in my ctor, but this didn't have any affect until I overrode getWidth() and getHeight().

    Why is this?

    Thanks,
    Andy

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You will likely want to set the preferredSize of the JPanel since most layout managers will use this to decide how to size the component. It may not work because you are not using layout managers, or not using layout managers that honor the preferredSize, or are not packing your gui before setting it visible.
    Last edited by Fubarable; 03-13-2011 at 03:19 AM.

  3. #3
    NoSwing is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You will likely want to set the preferredSize of the JPanel since most layout managers will use this to decide how to size the component. It may not work because you are not using layout managers, or not using layout managers that honor the preferredSize, or are not packing your gui before setting it visible.
    I'm using NetBeans for development and am not, at this point, using layout managers directly. I assumed that NetBeans had put that in there for me and was managing it. Is this a bad assumption?

    Your last statement has me intrigued. Again, I've been letting NetBeans manage the work of displaying the GUI. In all of the example code I've seen from the myriad of places I've been to looking for help, I've always seen calls to pack() before setting visible to true. Again, because I am using NetBeans, I assumed that this was being done for me. Is this too a bad assumption? Should the container always be packed if something is added to it? That is, even if it's already been displayed once before?

    Andy

Similar Threads

  1. Replies: 2
    Last Post: 12-13-2010, 01:34 PM
  2. Replies: 3
    Last Post: 12-13-2010, 07:22 AM
  3. Replies: 3
    Last Post: 04-21-2009, 11:01 PM
  4. How can i get image dimensions using java?
    By Thilkumar82 in forum Advanced Java
    Replies: 0
    Last Post: 09-02-2008, 09:12 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
  •