Java GUI Framework Question:
Download Questions PDF

How to get the screen size? How Do I center a window on the screen?

Java GUI Framework Interview Question
Java GUI Framework Interview Question

Answer:

Manually, pre 1.4:

Use java.awt.Toolkit.getDefaultToolkit().getScreenSize() to get the screen size, and do the math:

import java.awt.*;
Dimension winSize = win.getSize();
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
win.setLocation(
screenSize.width / 2 - winSize.width / 2,
screenSize.height / 2 - winSize.height / 2
);

Since 1.4:

Window.setLocationRelativeTo(null);

Download Java GUI Framework Interview Questions And Answers PDF

Previous QuestionNext Question
How to replace the icon in the title bar (window decoration) of a [J]Dialog?What is the equivalent of AWTs Canvas in Swing?