1

Hi I am developing an desktop application using swing when i am executing the frame window it is displaying at the left side bottom of my screen can i customize that and display at in center of the screen?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Suresh
  • 501
  • 1
  • 5
  • 11

2 Answers2

3

"yes i am using gui builder"

Just put setLocationRelativeTo(null) after initComponents(), in the constructor.

public class MyFrame extends JFrame {
    public MyFrame() {
        initComponents();
        setLocationRelativeTo(null);
    }
}

JFrame as a subclass of Window inherits Window methods. The API can be seen here The method used above is listed as follows

  • public void setLocationRelativeTo(Component c) - Sets the location of the window relative to the specified component according to the following scenarios.
    If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen.
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

Following Code will provide you Width and Height of Your Screen

Toolkit toolkit=Toolkit.getDefaultToolkit();

toolkit.getScreenSize().width

toolkit.getScreenSize().height

Then Using This Height and Width You can set where to position Your frame with following function

this.setLocation(int x, int y)

//first variable x is position relative to left top of ur screen in horizontal direction

// second variable y is position relative to left top in vertical direction

user987339
  • 10,519
  • 8
  • 40
  • 45