Results 1 to 4 of 4
  1. #1
    vicky15 is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Need to Understand code to make window centralized

    I am in process of learning swing, below code is used to make a window centralized

    import java.awt.Dimension;
    import java.awt.Toolkit;
    import javax.swing.JFrame;
    public class CenterOnScreen extends JFrame {
    public CenterOnScreen() {
    setSize(300, 200);
    setTitle("CenterOnScreen");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Toolkit toolkit = getToolkit();
    Dimension size = toolkit.getScreenSize();
    setLocation(size.width/2 - getWidth()/2,
    size.height/2 - getHeight()/2);
    }
    public static void main(String[] args) {
    CenterOnScreen cos = new CenterOnScreen();
    cos.setVisible(true);
    }
    }

    However please helps with my doubts inline:-

    1.) With code-line Toolkit toolkit = getToolkit();
    I am under impression that we are making object of toolkit class however why we have not used new .i.e.
    Toolkit toolkit = new getToolkit();

    2.) Please let me know how does this line works...
    setLocation(size.width/2 - getWidth()/2,
    size.height/2 - getHeight()/2);

    Thanks in advance... :-)

    Thanks,
    Ruchir

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

    Default

    What I do to center a JFrame:
    • Make sure that I use layout managers to aid in the display of my components, often nesting JPanels (containers) each with its own layout.
    • Avoid calling setSize(...) on anything.
    • Call pack() on my JFrame after having added components to it.
    • Call setLocationRelativeTo(null) on the JFrame after having called pack()
    • Then call setVisible(true) on the JFrame.
    Last edited by Fubarable; 05-13-2011 at 11:38 PM.

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    Quote Originally Posted by vicky15 View Post
    1.) With code-line Toolkit toolkit = getToolkit();
    I am under impression that we are making object of toolkit class however why we have not used new .i.e.
    Toolkit toolkit = new getToolkit();
    The code you suggested wouldn't work - the 'new' keyword expects a class constructor, e.g. new Toolkit(), not a method. However, this isn't done because the toolkit must vary according to the operating system being used, so the appropriate toolkit must be created. This is done using the method 'getToolkit()' which presumably calls the static method Toolkit.getDefaultToolkit() and returns a Toolkit implementation suitable for that OS. This kind of method is known as a 'factory method' because it 'manufactures' a class implementation depending on some context.

    2.) Please let me know how does this line works...
    setLocation(size.width/2 - getWidth()/2,
    size.height/2 - getHeight()/2);
    It finds the centre of the screen horizontally and offsets it by half the width of the frame, and finds the centre of the screen vertically and offsets it by half the height of the frame. This gives the coordinates of the top left of the frame when centered on the screen. Displaying the frame at these coordinates will display it centered on the screen.

    Fubarable's centering trick is much simpler!
    Last edited by dlorde; 05-14-2011 at 12:14 AM.

  4. #4
    vicky15 is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    Thank you dlorde for detailed explanation!!!

    Fubarable... thanks for suggesting a better and eazy approch.

Similar Threads

  1. Understand Code
    By Quizzle23 in forum New To Java
    Replies: 9
    Last Post: 03-07-2011, 10:07 PM
  2. need to understand code
    By Masken2 in forum New To Java
    Replies: 2
    Last Post: 02-17-2011, 04:21 PM
  3. How do you make another window appear
    By robertbob in forum AWT / Swing
    Replies: 5
    Last Post: 05-22-2010, 07:05 PM
  4. understand the code
    By prof.deedee in forum New To Java
    Replies: 8
    Last Post: 11-11-2009, 02:43 AM
  5. Trying to understand this code
    By new2java2009 in forum New To Java
    Replies: 2
    Last Post: 09-09-2009, 07:18 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
  •