How to make swing app learn about screen resizing?

While my swing application is working, I am changing the screen size (for example, from 1024x768 to 800x600).

Is there any event that I can listen to receive a notification about this?

Alternatively, I could check the screen size every couple of seconds, but Toolkit.getScreenSize () continues to tell me the old value.
How can I get the real screen size after changing?

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

I appreciate your help.
Marton

+6
java screen swing toolkit size
source share
3 answers

The following worked for me, but I am 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 actually introduced) was as follows:

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

Obviously, if you have multiple screen devices, you should be very careful.

+2
source share

To calculate the screen size you can use (taken from GraphicsConfiguration javadoc)

  Rectangle virtualBounds = new Rectangle(); GraphicsEnvironment ge = GraphicsEnvironment. getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); for (int j = 0; j < gs.length; j++) { GraphicsDevice gd = gs[j]; GraphicsConfiguration[] gc = gd.getConfigurations(); for (int i=0; i < gc.length; i++) { virtualBounds = virtualBounds.union(gc[i].getBounds()); } } 

Then you can request width and height virtualBounds .

This will also work in the settings of multiple monitors.

As far as I know, there is no JDK support method for detecting changes, so the survey is your only bet or use JNA and your own code. If your top-level window is maximized, resizing the screen will also resize all windows with the maximum size so you can also listen to the changes. See This Sun Tutorial for examples of writing component listeners.

If you do not have a window with a maximized top level, you can also achieve the same effect by creating a hidden window with a maximum size. I can’t say for sure if this will work, but it's worth a try.

+1
source share

Screen Size Size = Toolkit.getDefaultToolkit (). getScreenSize ();

f.setSize (screenSize.width, screenSize.height);

-one
source share

All Articles