JDK-8175293 : Window size is not updated after setting location to display with different DPI
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 9
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2017-02-21
  • Updated: 2017-04-18
  • Resolved: 2017-02-27
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 10 JDK 9
10Fixed 9 b161Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
The issue is reproduced on Windows with 2 displays which have different DPI.

Steps to reproduce:
- Run the code below
The frame appears. It shows the current display scale and the frame size.
- Press the "Move to another screen" button

The window location is set to another display.
The window size is not updated according to the new display DPI scale.
The window native size should be java size * uiScale.
The window native size should be updated after moving the window to the another display with different DPI.

---------------
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SimpleDPITest {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(SimpleDPITest::creteAndShowGUI);
    }

    private static void creteAndShowGUI() {

        final JFrame frame = new JFrame() {

            @Override
            public void paint(Graphics g) {
                super.paint(g);
                g.setColor(Color.BLUE);
                g.setFont(g.getFont().deriveFont(32f));
                AffineTransform tx = getGraphicsConfiguration()
                        .getDefaultTransform();

                g.drawString(String.format("Scale: [%2.1f, %2.1f]", tx.getScaleX(), tx.getScaleY()), 50, 200);
                g.drawString(String.format("Size: [%d, %d]", getWidth(), getHeight()), 50, 250);
            }
        };

        frame.setSize(400, 400);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        JButton button = new JButton("Move to another screen");
        button.addActionListener((e) -> {
            GraphicsConfiguration config = frame.getGraphicsConfiguration();
            GraphicsDevice device = config.getDevice();

            GraphicsDevice[] devices = GraphicsEnvironment
                    .getLocalGraphicsEnvironment()
                    .getScreenDevices();

            boolean found = false;
            for (GraphicsDevice dev : devices) {
                if (!dev.equals(device)) {
                    found = true;
                    Rectangle bounds = dev.getDefaultConfiguration().getBounds();
                    System.out.printf("bounds: %s\n", bounds);
                    AffineTransform tx = config.getDefaultTransform();
                    int x = (int) Math.round(bounds.x / tx.getScaleX()) + 15;
                    int y = (int) Math.round(bounds.y / tx.getScaleY()) + 15;
                    frame.setLocation(x, y);
                    break;
                }
            }

            if (!found) {
                System.out.println("Another display not found!");
            }
        });

        frame.setLayout(new FlowLayout());

        frame.add(button);
        frame.setVisible(true);
    }
}
---------------
Comments
The review thread: http://mail.openjdk.java.net/pipermail/awt-dev/2017-February/012622.html The proposed fix: http://cr.openjdk.java.net/~alexsch/8175293/webrev.00
21-02-2017