Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This will basically make sure that the dialog "fits" into the specified screen by moving it to be within the bounds of the specified device and shrinking it so that it's left and bottom edges are within the bounds of the specified device.</p> <p>This looks at the devices bounds and insets to calculate the "safe" area that the dialog can reside.</p> <pre><code>public class TestScreenSize { public static void main(String[] args) { new TestScreenSize(); } public TestScreenSize() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } Test test = new Test(); test.setLayout(new BorderLayout()); test.setVisible(true); System.exit(0); } }); } public class Test extends JDialog { public Test() { setModal(true); setLocation(0, 0); setSize(2000, 2000); } @Override public void setBounds(int x, int y, int width, int height) { Rectangle bounds = getSafeScreenBounds(new Point(x, y)); if (x &lt; bounds.x) { x = bounds.x; } if (y &lt; bounds.y) { y = bounds.y; } if (width &gt; bounds.width) { width = (bounds.x + bounds.width) - x; } if (height &gt; bounds.height) { height = (bounds.y + bounds.height) - y; } super.setBounds(x, y, width, height); } } public static Rectangle getSafeScreenBounds(Point pos) { Rectangle bounds = getScreenBoundsAt(pos); Insets insets = getScreenInsetsAt(pos); bounds.x += insets.left; bounds.y += insets.top; bounds.width -= (insets.left + insets.right); bounds.height -= (insets.top + insets.bottom); return bounds; } public static Insets getScreenInsetsAt(Point pos) { GraphicsDevice gd = getGraphicsDeviceAt(pos); Insets insets = null; if (gd != null) { insets = Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration()); } return insets; } public static Rectangle getScreenBoundsAt(Point pos) { GraphicsDevice gd = getGraphicsDeviceAt(pos); Rectangle bounds = null; if (gd != null) { bounds = gd.getDefaultConfiguration().getBounds(); } return bounds; } public static GraphicsDevice getGraphicsDeviceAt(Point pos) { GraphicsDevice device = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice lstGDs[] = ge.getScreenDevices(); ArrayList&lt;GraphicsDevice&gt; lstDevices = new ArrayList&lt;GraphicsDevice&gt;(lstGDs.length); for (GraphicsDevice gd : lstGDs) { GraphicsConfiguration gc = gd.getDefaultConfiguration(); Rectangle screenBounds = gc.getBounds(); if (screenBounds.contains(pos)) { lstDevices.add(gd); } } if (lstDevices.size() &gt; 0) { device = lstDevices.get(0); } else { device = ge.getDefaultScreenDevice(); } return device; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload