void (@NotNull Window popupWindow, int newWidth, int newHeight) { final Rectangle screenRectangle = ScreenUtil.getScreenRectangle(popupWindow); // shift the x coordinate if there is not enough space on the right Point location = popupWindow.getLocation(); int delta = (location.x + newWidth) - (screenRectangle.x + screenRectangle.width); location.x -= Math.max(delta, 0); location.x = Math.max(location.x, screenRectangle.x); final Rectangle targetBounds = new Rectangle(location.x, location.y, newWidth, newHeight); ScreenUtil.cropRectangleToFitTheScreen(targetBounds); if (targetBounds.height != popupWindow.getHeight() || targetBounds.width != popupWindow.getWidth()) { popupWindow.setBounds(targetBounds); popupWindow.validate(); popupWindow.repaint(); } }