Java Swing How to do this so that the program starts on the right side of the screen?

By default, my Swing program starts at the top left of the monitor. Is there any way to do this so that it pops up on the right side?

How about two monitors? Can I make it appear on the right monitor?

Thanks.

Carlo

+7
java swing
source share
3 answers

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

Java API

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

Centering JFrames

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

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

yourJFrame.setLocation( GraphicsEnvironment.getMaximumWindowBounds().getWidth() - yourJFrame.getWidth(), 0); 
+4
source share

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

You can use GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices() to get all monitors, then call GraphicsDevice#getConfigurations and then GraphicsConfiguration#getBounds to get the size and location of each monitor, and then use high school geometry to find the "right" monitor. (Please note that the actual setup of multiple monitors may place them in an arbitrary location.)

+7
source share

To display in a different source location, see JFrame.setLocation()

To display on another screen, see the available information from GraphicsEnvironment.getScreenDevices() .

0
source share

All Articles