Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: JFrame.pack() leaves 10px margin

  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default JFrame.pack() leaves 10px margin

    I'm working on an application which essentially has a jpanel in a jframe, and then I call .pack() for jframe, and it does what it's suppose to except for a 10px margin on the right and bottom. Any clue why it is doing this? I can provide code if necessary.

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

    Default Re: JFrame.pack() leaves 10px margin

    I can provide code if necessary.
    That would be good - a SSCCE which creates a frame and a panel, adds the panel to the frame then packs and displays the frame.

    Also, this is really a Swing question: I'll move it to the AWT/Swing forum.

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: JFrame.pack() leaves 10px margin

    Okay, I included the constructors which might be useful below. Also, the full file is here:
    privatepaste.com :: Paste ID e905cab22c



    Java Code:
    ...
    
    public Client(String title, int width, int height)
    {
    	FPS = 0;
    	WIDTH = width;
    	HEIGHT = height;
    	mouseX = mouseY = 0;
    	input = new HashMap<String, Boolean>();
    	updates = new ArrayList<Updatable>();
    	renders = new ArrayList<Renderable>();
    	
    	setBackground(Color.BLACK);
    	setTitle(title);
    	panel = new ClientPanel(this, width, height);
    	add(panel);
    	pack();
    	setResizable(false);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	setVisible(true);
    	panel.addMouseListener(this);
    	panel.addMouseMotionListener(this);
    }
    
    ...
    
    public ClientPanel(Client client, int width, int height)
    {	
    	this.client = client;
    	WIDTH = width;
    	HEIGHT = height;
    	setFocusable(true);
    	requestFocus();
    	setBackground(Color.BLACK);
    	setPreferredSize(new Dimension(WIDTH, HEIGHT));
    	period = 20;
    }
    Last edited by AndrewM16921; 01-28-2012 at 01:57 AM.

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

    Default Re: JFrame.pack() leaves 10px margin

    Did you read the SSCCE link?

    You are getting a 10px margin because somewhere within your code you are setting such a margin. The problem is, therefore, to remove things that are not relevant in order to reveal where this margin setting is taking place. That is the strategy suggested by the SSCCE and it is particularly successful: many times you will solve a problem just by revealing it. If you are unsuccessful then you still win because you have code that will be clear to others who might want to look at it.

    As it is, people are rather reluctant to download code and undertake, on your behalf, the task of separating what is part of the problem and what is distracting context. More so if there are several hundred lines of code including dependencies on other classes not given which, together, define a class which is ... abstract!

    -----

    I realise that constructing a SSCCE takes work, but that's the point. It's productive work if it reveals what needs to change.

  5. #5
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: JFrame.pack() leaves 10px margin

    Yeah, I'm familiar with the SSCCE thing. I chopped it up and the problem doesn't persist, but I can't seem to isolate what's causing it so I'm working on that at the moment.

  6. #6
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: JFrame.pack() leaves 10px margin

    Okay, this is weird. Can the setVisible() method change the size...?

    Java Code:
    System.out.println(panel.getSize().toString());
    setVisible(true);
    System.out.println(panel.getSize().toString());
    printed this

    java.awt.Dimension[width=900,height=700]
    java.awt.Dimension[width=910,height=710]

    Right well, I fixed it by moving the pack() to the end. Solved.
    Last edited by AndrewM16921; 01-28-2012 at 02:40 AM.

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

  8. #8
    mrhid6 is offline Member
    Join Date
    Sep 2011
    Posts
    37
    Rep Power
    0

    Default Re: JFrame.pack() leaves 10px margin

    You want to do the set visible and set resizible befor packing

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

    Default Re: JFrame.pack() leaves 10px margin

    Quote Originally Posted by mrhid6 View Post
    You want to do the set visible and set resizible befor packing
    Whatever gave you that notion? it's not correct.

    If you post a guess, please don't phrase it as if it is an authoritative statement.

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

Similar Threads

  1. Replies: 3
    Last Post: 12-07-2011, 10:59 PM
  2. recursive directories deletion leaves random files
    By Jovaras in forum New To Java
    Replies: 11
    Last Post: 05-27-2011, 10:29 AM
  3. Two calls to JFrame.pack() required to get desired effect?
    By dark_perfect in forum AWT / Swing
    Replies: 1
    Last Post: 03-24-2011, 01:03 AM
  4. PDF Printing - Margin Problem
    By faoilean in forum Advanced Java
    Replies: 0
    Last Post: 01-24-2011, 10:15 AM
  5. Using frame.pack() for resizing JFrame
    By LianaN in forum AWT / Swing
    Replies: 0
    Last Post: 10-30-2010, 12:12 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
  •