JDialog on windows goes beyond the Windows taskbar. Any workarounds?

This question discusses a known bug in which JFrames propagate to the Windows taskbar. The response associated with the error message (which has various duplicates) provides a workaround. I found that the problem also applies to JDialogs. JFrame workaround does not apply. Is there a similar workaround to get JDialogs to behave on windows?

Code example:

import javax.swing.*; public class Demo extends JDialog { public Demo() { setSize(250,12500); setVisible(true); } public static void main(String[] args) { new Demo(); } } 

Edit:
It doesn't seem to be fixed in the JDK. This error report closes with the comment that "If the developer wants the windows to fully display on the screen, they should consider checking the screen insert themselves [as the solution below], and recompile their component in a different way or resize the window manually after calling the package () or use a layout manager that supports screen insertion (unlike the more common ones such as BorderLayout and GridBagLayout]. "

+4
source share
2 answers

Basically, make sure that the dialog β€œfits” the specified screen by moving it within the specified device and reducing it so that it remains, and the lower edges are within the specified device.

Here, device boundaries and inserts are considered to calculate the β€œsafe” area in which the dialog may be.

 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 < bounds.x) { x = bounds.x; } if (y < bounds.y) { y = bounds.y; } if (width > bounds.width) { width = (bounds.x + bounds.width) - x; } if (height > 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<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length); for (GraphicsDevice gd : lstGDs) { GraphicsConfiguration gc = gd.getDefaultConfiguration(); Rectangle screenBounds = gc.getBounds(); if (screenBounds.contains(pos)) { lstDevices.add(gd); } } if (lstDevices.size() > 0) { device = lstDevices.get(0); } else { device = ge.getDefaultScreenDevice(); } return device; } } 
+5
source

Well, there is no magic below, but as simple as max (a, b):

 import java.awt.Dimension; import java.awt.Frame; import java.awt.Toolkit; import javax.swing.JDialog; public class Demo extends JDialog { public Demo() { super((Frame)null, "Demo" ); setDefaultCloseOperation( DISPOSE_ON_CLOSE ); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int width = Math.min( 250, screenSize.width ); int height = Math.min( 12_500, screenSize.height ); pack(); setSize( width, height ); setVisible( true ); } public static void main( String[] args ) { new Demo(); } } 
+1
source

Source: https://habr.com/ru/post/1445878/


All Articles