Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: how to set my frame in the middle of the screen

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default how to set my frame in the middle of the screen

    sorry for posting lots of threads, i dont intend to spam but i need to finish this one as soon as possible, because i have to make 3 programs for this semester in 2 different subjects, i need urgent guides and answers..

    well for this one... how can i display this frame in the middle of the sreen?

    ------- what ever the size would be.. i want to display this frame in the middle of the screen in different monitor (screen), i dont want to set it just by calculating the dimension of MY monitor(screen), "what if i run this in our school..? those monitor's dimension is ranging from 20 someting or regular CRT monitor screen, i have 17 inch here... so i need some answers on how can i display this in the center of different monitors...

    public class InventoryServer extends JFrame {
     
        private Toolkit toolkit;
        private Dimension windowSize;
        private JPanel panelSample;
        private Image icon;
     
        public InventoryServer() {
     
            toolkit = Toolkit.getDefaultToolkit();
            icon = new ImageIcon("c:/pics/mercuryLogo.jpg").getImage().getScaledInstance(300, 300, 0);
            windowSize = toolkit.getScreenSize();
            panelSample = new JPanel();
        }
     
        public void setComponents(Container cont) {
     
     
        }
     
        public void initializeServer() {
     
            this.setLocationByPlatform(true);
            this.setIconImage(icon);
            this.setTitle("Inventory Server");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setComponents(this.getContentPane());
            this.setBounds(0, 0, 400, 500);
            this.setVisible(true);
        }
     
        public static void main(String[] args) {
     
            SwingUtilities.invokeLater(new Runnable() {
     
                InventoryServer iS = new InventoryServer();
     
                public void run() {
     
                    iS.initializeServer();
                }
            });
        }
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to set my frame in the middle of the screen

    You can get the monitor resolution using Toolkit.getDefaultToolkit().getScreenSize() to get the monitor resolution (returns a Dimension object). I'm not sure how this type of a program would work, though if you had multiple monitors hooked up to the same computer.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (January 21st, 2010)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: how to set my frame in the middle of the screen

    ahmm world... how bout this.. method..

    setLocationRelativeTo(null)... when i used this one... its makes the frame centered in the monitor screen...?

    is this fine...?

  5. The Following User Says Thank You to chronoz13 For This Useful Post:

    rinoabm (December 29th, 2011)

  6. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to set my frame in the middle of the screen

    Definitely... using the method setLocationRelativeTo(null) will set the position of your frame at the center.

Similar Threads

  1. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM
  2. Unmovable frame (window) -- setFocus
    By chronoz13 in forum AWT / Java Swing
    Replies: 2
    Last Post: December 7th, 2009, 04:00 AM
  3. how to modify a JList Frame?
    By JM_4ever in forum AWT / Java Swing
    Replies: 0
    Last Post: October 14th, 2009, 11:58 PM
  4. The Frame to be Center Position
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 10:36 AM
  5. Replies: 5
    Last Post: September 19th, 2009, 06:48 AM