How to detect the current display with Java?

I have 2 displays connected, so I can either launch my Java application on the primary or the secondary display.

The question is: How can I know which display contains my app window, i.e., is there a way to detect the current display with Java?

java.awt.Window is the base class of all top level windows (Frame, JFrame, Dialog, etc.) and it contains the getGraphicsConfiguration() method that returns the GraphicsConfiguration that window is using. GraphicsConfiguration has the getGraphicsDevice() method which returns the GraphicsDevice that the GraphicsConfiguration belongs to. You can then use the GraphicsEnvironment class to test this against all GraphicsDevices in the system, and see which one the Window belongs to.

Window myWindow = ....
// ...
GraphicsConfiguration config = myWindow.getGraphicsConfiguration();
GraphicsDevice myScreen = config.getDevice();
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
// AFAIK - there are no guarantees that screen devices are in order... 
// but they have been on every system I've used.
GraphicsDevice[] allScreens = env.getScreenDevices();
int myScreenIndex = -1;
for (int i = 0; i < allScreens.length; i++) {
    if (allScreens[i].equals(myScreen))
    {
        myScreenIndex = i;
        break;
    }
}
System.out.println("window is on screen" + myScreenIndex);

Java/Scala: How to determine the monitor sizes of multiple displays , I'm currently trying to find the right way to find the current monitor size, when you'​re writing a Java Swing application to work in a multiple-monitor  I’m currently trying to find the right way to find the current monitor size, when you’re writing a Java Swing application to work in a multiple-monitor configuration. I always use three monitors, so I can test this pretty easily. Java default screen device. This SO post says that this code works, but it doesn’t work for me on Mac OS X 10.10:

The method proposed by Nate does not work when another monitor has just been added to the system and the user repositions the Java window into that monitor. This is a situation my users frequently face, and the only way around it for me has been to restart java.exe to force it to reenumerate the monitors.

The main issue is myWindow.getGraphicsConfiguration().getDevice() always returns the original device where the Java Applet or app was started. You would expect it to show the current monitor, but my own experience (a very time consuming and frustrating one) is that simply relying on myWindow.getGraphicsConfiguration().getDevice() is not foolproof. If someone has a different approach that's more reliable, please let me know.

Performing the match for screens (using the allScreen[i].equals(myScreen) call) then continues to return the original monitor where the Applet was invoked, and not the new monitor where it might have gotten repositioned.

GraphicsDevice (Java Platform SE 7 ), Returns the current display mode of this GraphicsDevice . To determine how to interpret the value of the String , contact the vendor of your Java Runtime. Get Current Date & Time: java.time.LocalDateTime. The LocalDateTime.now () method returns the instance of LocalDateTime class. If we print the instance of LocalDateTime class, it prints current date and time both. System.out.println (java.time.LocalDateTime.now ()); System.out.println (java.time.LocalDateTime.now ());

Professional Android 4 Application Development, 491 Android can reorient the display for use in any orientation; however, the Sensor to determine the current display orientation relative to the natural orientation, You can find the current screen rotation using the getRotation method on the side default: break; } code snippet PA4AD_Ch12_Sensors/src/​MyActivity.java  Once you have the screen size as a Java Dimension object, you can get the height and width as follows: // the screen height screenSize.getHeight(); // the screen width screenSize.getWidth(); As you might guess, the screen size height and width values are both pixel values (given as Java double values).

Nate's solution seem to work in most, but not all cases, as I had to experience. perplexed mentions he had problems when monitors got connected, I had issues with "Win+Left" and "Win+Right" key commands. My solution to the problem looks like this (maybe the solution has problems on it's own, but at least this works better for me than Nate's solution):

GraphicsDevice myDevice = myFrame.getGraphicsConfiguration().getDevice();
for(GraphicsDevice gd:GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()){
    if(frame.getLocation().getX() >= gd.getDefaultConfiguration().getBounds().getMinX() &&
        frame.getLocation().getX() < gd.getDefaultConfiguration().getBounds().getMaxX() &&
        frame.getLocation().getY() >= gd.getDefaultConfiguration().getBounds().getMinY() &&
        frame.getLocation().getY() < gd.getDefaultConfiguration().getBounds().getMaxY())
        myDevice=gd;
}

ITV Handbook: Technologies and Standards, imply the same meaning as their corresponding Personal Java interfaces and physical characteristics of the current display device, and detect modifications  Press ↵ Enter. This displays a readout of the current Java version that is installed on your computer in the Command Prompt. Make sure you include a space after "java". If it doesn't display any information or there is an error while running the command, you might not have Java installed on your computer.

This works for me

    public static GraphicsDevice getWindowDevice(Window window) {
    Rectangle bounds = window.getBounds();
    return asList(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()).stream()

            // pick devices where window located
            .filter(d -> d.getDefaultConfiguration().getBounds().intersects(bounds))

            // sort by biggest intersection square
            .sorted((f, s) -> Long.compare(//
                    square(f.getDefaultConfiguration().getBounds().intersection(bounds)),
                    square(s.getDefaultConfiguration().getBounds().intersection(bounds))))

            // use one with the biggest part of the window
            .reduce((f, s) -> s) //

            // fallback to default device
            .orElse(window.getGraphicsConfiguration().getDevice());
}

public static long square(Rectangle rec) {
    return Math.abs(rec.width * rec.height);
}

How can I get the android window size using Java as programming , How to detect the screen size for different android phones? I want the coordinate values like startx , starty , endx , endy. With that I can use the swipe method which​  To get equivalent information in Java, use Locale.getDefault () to get the Locale that Java is using, and use methods on the Locale object such as getCountry (), getLanguage () to get details. The information is available using ISO codes and as human readable/displayable names.

Edits, Display category: Presentation displays. This category can be used to identify secondary displays that are suitable for use as presentation  In fact, the String format helpers in Resources.java use the default locale[0] so if the string from the AssetManager comes from a locale that does not match the device's default locale (because your app does not support it but supports e.g. the next on the list) then there's a bug. – dcow Aug 2 '19 at 0:08

Monitor the Battery Level and Charging State, Determine the current charging state; Monitor changes in charging state; Determine the current battery level; Monitor significant changes in  SOLUTION. If you recently installed Java, you may need to restart your browser (close all browser windows and re-open), in order for the browser to recognize the installation. In addition, make sure Java content in the browser is enabled through the Java Control Panel .

Present web pages to secondary attached displays, Chrome 66 allows web pages to use a secondary attached display through Let me walk you through how to use the Presentation API to present a web page on your secondary attached display. Check out the official Chrome sample we've used for this article. Java is a registered trademark of Oracle and/or its affiliates. Java is disabled or not installed in this browser This page tells you if Java is installed and enabled in your current web browser and what version you are running. Java detection relies on JavaScript being enabled. So if you don't have JavaScript enabled then it's not possible to detect if Java is installed and enabled.

Comments
  • please check my answer... it may or may not help you
  • You can substantially simplify and clarify that code by using Rectangle.contains.