Suresh
Suresh

Reputation: 559

how to customize and display the frame window at center of my screen in java swing?

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?

Upvotes: 1

Views: 191

Answers (2)

user3386797
user3386797

Reputation: 3

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

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 208974

"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.

Upvotes: 3

Related Questions