Results 1 to 9 of 9
  1. #1
    hiranya is offline Member
    Join Date
    Jun 2007
    Location
    Colombo, Sri Lanka
    Posts
    32
    Rep Power
    0

    Default JFrame at Center Screen

    Hi,

    I want a JFrame to always appear at the center of the screen. Is there a way to achieve this?

    Regards,
    Hiranya

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    Java Code:
        // Get the size of the screen
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        
        // Determine the new location of the window
        int w = window.getSize().width;
        int h = window.getSize().height;
        int x = (dim.width-w)/2;
        int y = (dim.height-h)/2;
        
        // Move the window
        window.setLocation(x, y);

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    If you are using j2se 1.4+ you have access to the Window method
    setLocationRelativeTo(Component c). Use null for the argument to center your top–level container.

  4. #4
    Join Date
    Oct 2009
    Posts
    88
    Rep Power
    0

    Default

    use this.setLocationRelativeTo(getRootPane());

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

    Default

    Quote Originally Posted by anilkumar_vist View Post
    use this.setLocationRelativeTo(getRootPane());
    You resurrected a zombie to post this rubbish? Eejit.

    db

  6. #6
    whocares is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    You resurrected a zombie to post this rubbish? Eejit.

    db
    And you replied just to put this?

    Perhaps you could have tried adding something useful, rather than just "Eejit"?

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Something useful to a rotten dead thread which has been answered three years ago? Come on.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    Perhaps you could have tried adding something useful, rather than just "Eejit"?
    It was usefull. It told anyone reading this posting to ignore that suggestion.

    The proper solution has already been given so there was no need to repeat it.

    Why don't you try (as a new member to the forum) to contribute something positive.

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

Similar Threads

  1. new screen
    By tha_crazy in forum New To Java
    Replies: 4
    Last Post: 02-16-2011, 10:46 AM
  2. Center background image
    By Floetic in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2008, 05:50 PM
  3. center a form
    By tommy in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:47 PM
  4. Help Center Live 2.1.3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-06-2007, 03:43 PM
  5. Full screen
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 05:49 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
  •