10

I have a Java app that launches, creates a GUI and works great. If the user changes the screen resolution (switches from 1440x900 to 1280x768), I'd like to be able to listen for that event.

Any ideas?

PS - I'd like to do this in event/listener mode, not in poll mode, so that extra CPU cycles aren't wasted on something like a timer constantly polling the screen size every N seconds to see if it's changed.

Rick Hodgin
  • 657
  • 2
  • 8
  • 15

5 Answers5

4

This post is old, but: - polling the screen size once every second will not have any impact on performance - when the screen is resized, every window should receive a repaint() call (you need to test this for the OSs you target)

JayC
  • 41
  • 1
2

I don't think that Java can do this by itself. You would have to have a "hook" into the operating system that detects this change and may wish to consider using JNA, JNI, or a separate utility such as perhaps Sigar that reports to your Java program. Out of curiosity, why do you wish to do this? Is it for a game you're making or so that you can re-size a GUI application?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Hovercraft (nice MP reference), I had thought so. With JNI I can hook the WinProc, intercepting all WM_* messages, and track it that way. Was hoping there was a native function though, as it would seem desirable for a Java app to know such a thing, hence it would be provided by the JVM and native listeners. – Rick Hodgin Sep 19 '11 at 12:36
2

Apart from Hovercrafts's suggestion you might consider a background thread that checks the current screen resolution using Toolkit.getScreenSize().

You would need to test this to find out how big the performance impact of that thread to the system is. How often it checks for changes depends on your requirements (how quick your application needs to react to the change)

1

You can create a global PAINT listener to detect screen resize.

// screen resize listener
    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

        @Override
        public void eventDispatched(AWTEvent event) {
// take a look at http://stackoverflow.com/questions/10123735/get-effective-screen-size-from-java
            Rectangle newSize = getEffectiveScreenSize(); 
            if (newSize.width != windowSize.width || newSize.height != windowSize.height)
                resize();

        }
    }, AWTEvent.PAINT_EVENT_MASK);
acimutal
  • 2,155
  • 2
  • 17
  • 22
-1

Here is my suggestion for this problem,

  1. Every swing object can implement an interface called java.awt.event.ComponentListener.
  2. One of its method is componentResized(ComponentEvent e).
  3. Your main application frame should implement this interface and override the resize event method. This is how you listen to the resize event, Checkout this link . I hope this helps you.
Santosh
  • 17,667
  • 4
  • 54
  • 79
  • 1
    As I understand it, the main frame would be resized if the user resized the window, or if the window was maximized and the user changed the video mode, and the componentResized() would be called in such a case. But in the case of a non-maximized window of size X, a change in video mode will not force that window (frame) to resize itself, and instead, once the video mode is resized, it will simply hang off the edges of the screen at its original size, having never received the componentResized() call. – Rick Hodgin Sep 19 '11 at 17:25
  • Probably downvoted because you're solving the wrong problem: As Rick wrote, ComponentListener gets called when the user changes window size, not when screen resolution changes. – toolforger Mar 29 '18 at 09:49