Search code examples
javaswingsizescreentoolkit

How to make a swing app aware of screen size change?


while my swing app is running I change the size of the screen (e.g. from 1024x768 to 800x600).

Is there any event I can listen to to be notified about this?

Alternatively I could check the screen size in every couple of second, but the Toolkit.getScreenSize() keeps telling me the old value.
How could I get the real screen size after the change?

Environment: Linux (tested on SuSE ES 11 and Ubuntu 9.04)

I appreciate your help.
Marton


Solution

  • The following worked for me, but I'm on a Mac, so I can't say for sure that it will work on Linux:

    System.out.println(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getWidth() + "x" + GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getHeight());
    
    //Ignore this block, it was simply to give me a chance to change my resolution
    Scanner readUserInput=new Scanner(System.in);
    System.out.println("Waiting on input, go change your resolution");
    String myName=readUserInput.nextLine();
    
    System.out.println(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getWidth() + "x" + GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getHeight());
    

    The output (blah is actually input) was as follows:

    1440x900
    Waiting on input, go change your resolution
    blah
    1280x960
    

    Obviously if you have multiple screen devices, you will have to exercise greater caution.