tags:

views:

73

answers:

3

By default, my Swing program starts at the top left hand side of the monitor. Is there a way to make it so it pops up at the right hand side?

What about dual monitors? Can I get it to pop on the right monitor?

Thanks.

Carlo

+2  A: 

Assuming you don't want to cover up the taskbar, you should use getMaximumWindowBounds.

Java API

http://www.javabeginner.com/java-swing/java-jframe-class-example

Centering JFrame’s

By default, a Jframe is displayed in the upper-left corner of the screen. To display a frame at a specified location, you can use the setLocation(x, y) method in the JFrame class. This method places the upper-left corner of a frame at location (x, y).

Your pseudo code for the top right corner looks something like this:

yourJFrame.setLocation(
  GraphicsEnvironment.getMaximumWindowBounds().getWidth() - 
  yourJFrame.getWidth(), 0);
Thomas Langston
Thanks! This got me started--- the answer by Geoffrey Zheng solved my second problem.
Carlo del Mundo
A: 

For displaying at another initial location, take a look at JFrame.setLocation()

For displaying on another screen, take a look at avaible infos from GraphicsEnvironment.getScreenDevices().

Riduidel
+5  A: 

There are many questions/answers about using GraphicsEnvironment to do similar things.

You can use GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices() to get all the monitors, then call GraphicsDevice#getConfigurations and then GraphicsConfiguration#getBounds to get the size and location of each monitor, then use your high-school geometry to find the "right" monitor. (Note that a true multi-monitor setup can put them in arbitrary location.)

Geoffrey Zheng
Is it guaranteed that the first device under GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices() will return the first most monitor? I ask because using 'high-school geometry' is possible, but not efficient if implementing in multi monitor platforms where monitors vary from 1 through 10.
Carlo del Mundo
"First most" monitor is ill-defined because as I said you can position multiple monitors any way you want, for example 4 monitors at the 4 vertexes of a diamond--is the top one "first most" or the left one? You can use `GraphicsEnvironment#getDefaultScreenDevice` to get the, you know, *default* monitor, but you can set any monitor to be default (at least in Windows).
Geoffrey Zheng