Results 1 to 8 of 8

Thread: JFrame Size

  1. #1
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default JFrame Size

    When one sets the size of a JFrame, for example

    JFrame frame = new JFrame();
    frame.setSize( 400, 600 );

    what is the actual size of the associated content pane? In the above example, what is 400 px by 600 px?

    If I set a box layout for the content pane, and add components with a sum of heights equal to 600 px, the content pane appears too small.

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

    Default Re: JFrame Size

    You can always check the insets to see, but myself, I rarely if ever set a JFrame's size. Usually I let my components size themselves and call pack on the JFrame. Occasionally I'll set the preferredSize of a component or if extending a component override its getPreferredSize method (to avoid the wrath of Kleopatra), and again call pack() on the JFrame after adding all components.

  3. #3
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: JFrame Size

    Pack() is problematic in the current context. The frame needs to be a set size to work in a larger context.

    If I get the insets, it returns zeros.

    This leads me to believe there are no margins, but then why do the properly sized components not work?

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

    Default Re: JFrame Size

    Quote Originally Posted by newbie123 View Post
    Pack() is problematic in the current context. The frame needs to be a set size to work in a larger context.
    I'm having difficulty understanding just what your problem is. Consider providing us with more information. What exactly are you trying to do? What exactly would you like your GUI to look like? Can you provide a picture? Can you provide compilable runnable code that has none of your program logic but that creates a facsimile of your application so that we can modify and possibly improve it?

  5. #5
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: JFrame Size

    The GUI needs to be a fixed size; in this case it needs to be about 400 px by 600 px. If I set the layout for the frame's content pane to BoxLayout, and a populate the content pane with components that are 600 px in height, it doesn't fit. (The components min, preferred, and max heights are al set.)

    Does this help? If not, I can cobble together an example to demonstrate the problem.

    I really just want to know if setSize() sets the size of the overall frame (title bar, edges, etc.), or does act on the content pane, root pane, etc.

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

    Default Re: JFrame Size

    A Picture would help, a description of what the GUI is for, what visually you're trying to achieve, and again, some code would help. We'd appreciate any and all information that helps us to understand the problem. As for setSize, again I really don't use it. You could set the preferredSize of the contentPane or the JFrame itself if desired and then call pack(). pack is important as it helps your layout managers do initialize the layouts.

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

    Default Re: JFrame Size

    Java Code:
    getContentPane().getSize()
    Or as appropriate
    Java Code:
    getContentPane().setPreferredSize(...);
    pack();
    as suggested by Fubarable.
    db

  8. #8
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  2. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 04:37 PM
  3. JFrame 's components size and location problem
    By petrosgraf in forum Threads and Synchronization
    Replies: 5
    Last Post: 04-18-2009, 02:24 AM
  4. Image size in a JFrame
    By nickbeacroft in forum AWT / Swing
    Replies: 2
    Last Post: 06-26-2008, 04:08 PM
  5. Listener for JFrame size change
    By Thez in forum AWT / Swing
    Replies: 10
    Last Post: 02-14-2008, 03:10 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
  •