20060928 Thursday September 28, 2006

Centering Windows on the Screen

Here is a nifty Java trick. I wanted to center a JDialog relative to the physical screen, vs. having it appear in the upper left corner or aligned relative to another component. After a quick web search, I found the following tidbit which takes care of the issue in a single line of code (assuming you have previously created a JDialog called "dialog"):

dialog.setLocationRelativeTo(null);

Works for JFrames too, and should work for anything that with java.awt.Window as a parent, though I have only tested this with JDialog and JFrame.

Posted by rickg ( Sep 28 2006, 02:09:36 AM PDT ) Permalink Comments [0]