28-07-2009, 02:07 PM
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:
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:
Now, whenever you want to properly center a JPane or window, just use this:
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
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