Search code examples
javaswingscreenresolution

How do I determine the resolution of the screen where the JFrame resides?


I wish to determine the screen resolution where my java application is currently showing.

For example, lets say a user has my java application on his second monitor and the two monitors have the specifications of: primary 1024x1280 (Portrait rotation), secondary 1280x1024 (Landscape rotation).

I want to make the height of my popup JList fit within a comfortable margin of the second monitor. I won't get the correct results from Toolkit#getDefaultToolkit()#getScreenSize() as this would give me the dimensions from the primary monitor.

I could get the size of all the monitors by using GraphicsEnvironment#getScreenDevices() but how do I figure out which of these devices my JFrame is running within?

Thanks for your time.

-Dennis


Solution

  • Assuming frame is your JFrame, you can simply use the following code to retrieve the GraphicsDevice:

    frame.getGraphicsConfiguration().getDevice();
    

    From there, you can get the whole the information of the screen on which the frame is displayed.