Java:Open Your Software in the Middle of the Desktop

By default Java Programs(GUI based)actually starts/Open  in the side of the desktop.  IF you want your java program/software (where GUI such as JFrame or JPanel is used) to open by default in the middle of the desktop then it’s for you.

Here I have given the code which will help you to open your java (JFrame window) in the middle of the desktop.

Whatever the resolution of your desktop monitor has, this code will help you to open your desire java written software exactly in the middle of the monitor.

Code:

import java.awt.*;

import javax.swing.*;

public class middlePosition extends JFrame

{

private Toolkit  toolkit;

                                                public middlePosition()

                                                {

                                                JPanel panel=new JPanel();

                                                getContentPane().add(panel);

                                                panel.setLayout(null);

                                                setSize(500,500);

                                                setTitle(“Set JFrame Window in the middle of the Desktop”);

                                                setDefaultCloseOperation(EXIT_ON_CLOSE);

                                                toolkit=getToolkit();

                                                Dimension size=toolkit.getScreenSize();

                                                setLocation(size.width/2-getWidth()/2, size.height/2-getHeight()/2);

                                                }

                        public static void main(String args[])

                        {

                        middlePosition x=new middlePosition();

                        x.setVisible(true);

                        }

}

OUTPUT:

Java program in the middle of the desktop

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: