Custom Monitor Resolution not recognized by Java

Posted by Angie on Stack Overflow See other posts from Stack Overflow or by Angie
Published on 2010-03-28T14:12:25Z Indexed on 2010/03/28 14:23 UTC
Read the original article Hit count: 326

Filed under:
|
|

My weird monitor's native resolution isn't recognized by Windows, so I have to set a custom resolution for it. The problem is that java doesn't recognize it since it's not on Win7's "approved" list, so full-screen mode gets "stuck". Netbeans comes out of full-screen fine, so there has to be a way around this. Anyone know it?

This example reproduces the issue:


package resolutionexample;

import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                GraphicsDevice gd = ge.getDefaultScreenDevice();

                DisplayMode currentDM = gd.getDisplayMode();

                boolean currentInAvailable = false;

                System.out.println("Available resolutions:");
                for ( DisplayMode availDM : gd.getDisplayModes() ){
                    //System.out.println(availDM.getWidth() + "x" + availDM.getHeight());
                    if ( availDM.equals(currentDM) ){
                        currentInAvailable = true;
                    }
                }

                System.out.println("Current resolution: " + currentDM.getWidth() + "x" + currentDM.getHeight() );

                System.out.println("Current in available: " + currentInAvailable);


                JFrame frame = new JFrame("Resolution Bug Example");
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                if ( !gd.isFullScreenSupported() ){System.exit(0);}

                gd.setFullScreenWindow(frame);

                gd.setFullScreenWindow(null);
            }
        });
    }
}

Output running 1680x1050 (the monitor's wonky native resolution):


run:
Available resolutions:
Current resolution: 1680x1050
Current in available: false
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid display mode
        at sun.awt.Win32GraphicsDevice.setDisplayMode(Win32GraphicsDevice.java:393)
        at sun.awt.Win32GraphicsDevice.setFullScreenWindow(Win32GraphicsDevice.java:329)
        at resolutionexample.Main$1.run(Main.java:43)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 2 seconds)

Output if I set my resolution to 1024x768 before running:


run:
Available resolutions:
Current resolution: 1024x768
Current in available: true
BUILD SUCCESSFUL (total time: 2 seconds)

© Stack Overflow or respective owner

Related posts about java

Related posts about swing