provide-free-support Ujjwal's Blog: Centering a swing window on screen

Wednesday, September 22, 2010

Centering a swing window on screen

In swing or AWT, if you initialize a frame or window by default its position start from the top left corner of screen. Some time it looks odd as we are intended to see the window/frame at center of screen.
To make a window centered, we all need to set the location of window to center with respect to screen size.
There are following steps to make a window centered.
1. Get the screen size
2. Set the window/frame location by calculating it with screen size and frame size.
See the following example.
centerOnScreen Method provide the functionality to make a window centered.


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
*
* @author ujjwal soni
*
*/
public class CenteredFrameExample {
JFrame frame;
JLabel label;
public CenteredFrameExample() {
frame = new JFrame("Centered JFrame.");
label = new JLabel();
frame.setSize(250, 150);
frame.setPreferredSize(new Dimension(250, 150));
frame.getContentPane().add(BorderLayout.CENTER, label);
centerOnScreen(frame);
frame.setVisible(true);
frame.pack();
}
public static void main(String args[]) {
CenteredFrameExample centeredFrame = new CenteredFrameExample();
String text = "This is the centered frame...
"; centeredFrame.label.setText(text); } /** * Centers a window on screen. * * @param w The window to center. */ public void centerOnScreen(Window w) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); w.setLocation(screenSize.width / 2 - (w.getWidth() / 2), screenSize.height / 2 - (w.getHeight() / 2)); } }
 
Thats all for now....
Cheers!!!
Ujjwal Soni

No comments:

best-it-exam-    | for-our-work-    | hottst-on-sale-    | it-sale-    | tast-dumps-us-    | test-king-number-    | pass-do-it-    | just-do-it-    | pass-with-us-    | passresults-everything-    | passtutor-our-dumps-    | realtests-us-exam-    | latest-update-source-for-    | cbtnuggets-sale-exam    | experts-revised-exam    | certguide-sale-exam    | test4-sale-exam    | get-well-prepared-    | certkiller-sale-exam    | buy-discount-dumps    | how-to-get-prepared-for-the    | in-an-easy-way    | brain-dumps-sale    | with-pass-exam-guarantee    | accurate-study-material    | at-first-try    | 100%-successful-rate    | get-certification-easily    | material-provider-exam    | real-exam-practice    | with-pass-score-guarantee    | certification-material-provider    | for-certification-professionals    | get-your-certification-successfully    | 100%-Pass-Rate    | in-pdf-file    | practice-exam-for    | it-study-guides    | study-material-sku    | study-guide-pdf    | prep-guide-demo    | certification-material-id    | actual-tests-demo    | brain-demos-test    | best-pdf-download    | our-certification-material    | best-practice-test    | leading-provider-on    | this-course-is-about    | the-most-reliable    | high-pass-rate-of    | high-pass-rate-demo    | recenty-updated-key    | only-for-students-free-download    | courseware-plus-kit-for    | accurate-answers-of    | the-most-reliable-id    | provide-training-for    | welcome-to-buy    | material-for-success-pass    | provide-free-support    | best-book-for-pass    | accuracy-of-the-answers    | pass-guarantee-id    | pass-exam-key    | pass-sku-id    | study-for-exid    | pdf-sku-exid    | sku-pdf-id    | pdf-demo-key    | exam-demo-ske    | pass-it-dump    |
http://www.menuiserie-dauvergne.com/    | http://www.menuiserie-dauvergne.com/    |