Mirage Source
http://web.miragesource.net/forums/

Thought for the day: Centering a Java JPane properly
http://web.miragesource.net/forums/viewtopic.php?f=142&t=6055
Page 1 of 1

Author:  Coke [ Tue Jul 28, 2009 2:07 pm ]
Post subject:  Thought for the day: Centering a Java JPane properly

Simple right?

Well, I developed a little something on my laptop last night then brought it to my dual screen linux setup here... the default approach places the JPane in the center of both my screens.. so half on both... interesting. By default approach, I mean:

Code:
Toolkit toolkit = Toolkit.getDefaultToolkit(); 
Dimension screenResolution = toolkit.getScreenSize();

JFrame mainWindow = new JFrame("Main Window"); 
mainWindow.setSize(300, 350); 

int x = (screenResolution.width - mainWindow.getWidth()) / 2; 
int y = (screenResolution.height - mainWindow.getHeight()) / 2; 

mainWindow.setLocation(x, y);


So we are grabbing the screen size from the JVM which is fine, unfortunately in linux (not sure if it treats it differently in windows) this happens to be the overall size of all your monitors in their current configuration - I'm guessing this is due to the way X (not the above variable, the linux display thing) handles everything.

Anyway, you can actually tell Java to give you all of the display devices it can find (this can be anything from a monitor to a printer...), find out which ones are raster devices (thus eliminating LEDs, printers etc) and grab the default system display of that type - our users primary monitor. You can then ask for the specifics of that monitor, plot a rectangle object with those vars, and offset your window to that instead - ensuring your app always opens bang in the middle of their primary monitor. Yay.

Here is my code:

Code:
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;

public class GUIUtils {
   
   public static Point getCenterLocation(Dimension d){      
      Point center = new Point();
      Rectangle screenResolution = getPrimaryScreen().getBounds();
      
      center.x = (screenResolution.width - d.width) / 2;
      center.y = (screenResolution.height - d.height) / 2;
      
      return (center);
   }
   
   public static GraphicsConfiguration getPrimaryScreen(){
      GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
      GraphicsDevice[] devs = env.getScreenDevices();
      GraphicsConfiguration config = null;
      
      int size = (devs == null) ? 0 : devs.length;
      for (int i = 0; i < size && config == null; i++)
      {
         int type = devs[i].getType();
         if(type == GraphicsDevice.TYPE_RASTER_SCREEN)
         {
            config = devs[i].getDefaultConfiguration();
         }
      }
      
      config.getBounds();
      
      return(config);
   }

}


Now, whenever you want to properly center a JPane or window, just use this:

Code:
yourJPaneName.setLocation(GUIUtils.getCenterLocation(yourJPaneName.getSize()));


Super simple and very handy. A nice little piece of code I'll keep hold of for a good while I think, true cross-platform consideration :3

Fox

Author:  grimsk8ter11 [ Tue Jul 28, 2009 7:00 pm ]
Post subject:  Re: Thought for the day: Centering a Java JPane properly

Windows, as far as I know, treats each screen as a seperate entity, and split programs, when maximized and such, default to the primary screen. Though, there are some programs that allow Windows to treat them both as one screen, so they may treat it differently.

Author:  unknown [ Thu Jul 30, 2009 9:31 pm ]
Post subject:  Re: Thought for the day: Centering a Java JPane properly

If your looking for the easy solution to centering just...
Code:
JFrame myFrame;
// Add controls...
myFrame.pack();
myFrame.setLocationRelativeTo(null);

I doubt it will center it on the primary monitor but it's better than coke's default approach :D.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/