/** * Show the given message in a dialog box or independent window, depending on whether the source * component is contained in a Frame or not. * * @param c The Controller that calls this method, or null if it is not called by a Controller. * (The Controller, if any, will be notified when the error message is cleared.) * @param message The message to display. */ public void setErrorMessage(Controller c, String message) { if (popup != null) clearErrorMessage(); if (message == null) return; errorSource = c; errorMessage = message; Component parent = source; while (parent != null && !(parent instanceof Frame)) parent = parent.getParent(); if (parent != null) popup = new Dialog((Frame) parent, "Error Message", true); // modal dialog else popup = new Frame("Error Message"); // independent window popup.setBackground(Color.white); popup.add(new MC(message), BorderLayout.CENTER); Panel buttonBar = new Panel(); buttonBar.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10)); Button OK = new Button(" OK "); OK.addActionListener(this); buttonBar.add(OK); popup.add(buttonBar, BorderLayout.SOUTH); popup.pack(); if (parent == null) popup.setLocation(100, 80); else popup.setLocation(parent.getLocation().x + 50, parent.getLocation().y + 30); popup.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent evt) { popup.dispose(); } }); popup.show(); // make the dialog visible. }
private void showJPopupMenu(MouseEvent e) { try { if (e.isPopupTrigger() && menu != null) { if (window == null) { if (isWindows) { window = new JDialog((Frame) null); ((JDialog) window).setUndecorated(true); } else { window = new JWindow((Frame) null); } window.setAlwaysOnTop(true); Dimension size = menu.getPreferredSize(); Point centerPoint = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); if (e.getY() > centerPoint.getY()) window.setLocation(e.getX(), e.getY() - size.height); else window.setLocation(e.getX(), e.getY()); window.setVisible(true); menu.show(((RootPaneContainer) window).getContentPane(), 0, 0); // popup works only for focused windows window.toFront(); } } } catch (Exception ignored) { } }
/** * Uninstalls the sheet on the owner. This method is invoked immediately after the JSheet is * hidden. */ protected void uninstallSheet() { if (isInstalled) { Window owner = getOwner(); if (owner != null) { if (isExperimentalSheet()) { owner.removeWindowListener(windowEventHandler); } else { // Note: We mustn't change the windows focusable state // because // this also affects the focusable state of the JSheet. // owner.setFocusableWindowState(true); owner.setEnabled(true); // ((JFrame) owner).setResizable(true); owner.removeComponentListener(ownerMovementHandler); if (shiftBackLocation != null) { owner.setLocation(shiftBackLocation); } if (oldFocusOwner != null) { owner.toFront(); oldFocusOwner.requestFocus(); } } } isInstalled = false; } }
/** * Tries to load/restore the window state of the given window. * * @param aNamespace the namespace to use for the window state; * @param aProperties the properties to read from; * @param aWindow the window to load the state for. */ public static void loadWindowState(final Preferences aProperties, final Window aWindow) { // Special case: for FileDialog/JFileChooser we also should restore the // properties... loadFileDialogState(aProperties, aWindow); try { final int xPos = aProperties.getInt("winXpos", -1); final int yPos = aProperties.getInt("winYpos", -1); if ((xPos >= 0) && (yPos >= 0)) { aWindow.setLocation(xPos, yPos); } } catch (NumberFormatException exception) { // Ignore... } if (isNonResizableWindow(aWindow)) { // In case the window cannot be resized, don't restore its width & // height... return; } try { final int width = aProperties.getInt("winWidth", -1); final int height = aProperties.getInt("winHeight", -1); if ((width >= 0) && (height >= 0)) { aWindow.setSize(width, height); } } catch (NumberFormatException exception) { // Ignore... } }
public static void centerWindow(Window window) { Preconditions.checkArgument(window != null); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int x = ((int) screenSize.getWidth() - window.getWidth()) >> 1; int y = ((int) screenSize.getHeight() - window.getHeight()) >> 1; window.setLocation(x, y); }
/* * Places the Window object at the center of the screen */ public static void centerWindow(Window win) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension dlgSize = win.getSize(); win.setLocation( (screenSize.width - dlgSize.width) / 2, (screenSize.height - dlgSize.height) / 2); }
/** * Method description * * @param window */ public static void centerWindow(Window window) { Dimension dimension = window.getSize(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int x = (screenSize.width - dimension.width) / 2; int y = (screenSize.height - dimension.height) / 2; window.setLocation(x, y); window.requestFocus(); }
private static void setLocation( final RelativePoint p, final PopupComponent popup, Component content) { if (popup == null) return; final Window wnd = popup.getWindow(); assert wnd != null; wnd.setLocation(p.getScreenPoint()); }
public static Window moveTo( JComponent content, Point screenPoint, final Dimension headerCorrectionSize) { setDefaultCursor(content); final Window wnd = SwingUtilities.getWindowAncestor(content); if (headerCorrectionSize != null) { screenPoint.y -= headerCorrectionSize.height; } wnd.setLocation(screenPoint); return wnd; }
public static void setPositionRelativeToParent( Window w, Component parent, int hOffset, int vOffset) { try { Point p = parent.getLocationOnScreen(); int x = (int) p.getX() + hOffset; int y = (int) p.getY() + vOffset; w.setLocation(x, y); w.pack(); } catch (Throwable t) { } }
private void setSizeAndDimensions( @NotNull JTable table, @NotNull JBPopup popup, @NotNull RelativePoint popupPosition, @NotNull List<UsageNode> data) { JComponent content = popup.getContent(); Window window = SwingUtilities.windowForComponent(content); Dimension d = window.getSize(); int width = calcMaxWidth(table); width = (int) Math.max(d.getWidth(), width); Dimension headerSize = ((AbstractPopup) popup).getHeaderPreferredSize(); width = Math.max((int) headerSize.getWidth(), width); width = Math.max(myWidth, width); if (myWidth == -1) myWidth = width; int newWidth = Math.max(width, d.width + width - myWidth); myWidth = newWidth; int rowsToShow = Math.min(30, data.size()); Dimension dimension = new Dimension(newWidth, table.getRowHeight() * rowsToShow); Rectangle rectangle = fitToScreen(dimension, popupPosition, table); dimension = rectangle.getSize(); Point location = window.getLocation(); if (!location.equals(rectangle.getLocation())) { window.setLocation(rectangle.getLocation()); } if (!data.isEmpty()) { TableScrollingUtil.ensureSelectionExists(table); } table.setSize(dimension); // table.setPreferredSize(dimension); // table.setMaximumSize(dimension); // table.setPreferredScrollableViewportSize(dimension); Dimension footerSize = ((AbstractPopup) popup).getFooterPreferredSize(); int newHeight = (int) (dimension.height + headerSize.getHeight() + footerSize.getHeight()) + 4 /* invisible borders, margins etc*/; Dimension newDim = new Dimension(dimension.width, newHeight); window.setSize(newDim); window.setMinimumSize(newDim); window.setMaximumSize(newDim); window.validate(); window.repaint(); table.revalidate(); table.repaint(); }
public static Window setSize(JComponent content, final Dimension size) { final Window popupWindow = SwingUtilities.windowForComponent(content); final Point location = popupWindow.getLocation(); popupWindow.setLocation(location.x, location.y); Insets insets = content.getInsets(); if (insets != null) { size.width += insets.left + insets.right; size.height += insets.top + insets.bottom; } content.setPreferredSize(size); popupWindow.pack(); return popupWindow; }
/** Handles mouse dragged event */ public void mouseDragged(MouseEvent ev) { Window w = (Window) ev.getSource(); if (isMovingWindow) { Point windowPt; try { windowPt = (Point) AccessController.doPrivileged(getLocationAction); windowPt.x = windowPt.x - dragOffsetX; windowPt.y = windowPt.y - dragOffsetY; w.setLocation(windowPt); windowMoved = true; } catch (PrivilegedActionException e) { } } }
/** * Show the window. * * @param container - Window of JFrame to show */ private void showPopup(Window container) { if (visibleComponent.isEnabled()) { Point pt = visibleComponent.getLocationOnScreen(); pt.translate(0, visibleComponent.getHeight()); container.setLocation(pt); container.toFront(); ApplicationManager.setCurrentlySelectedField(fieldName); if (container instanceof OntologySelector) { ((OntologySelector) container).makeVisible(); } else { container.setVisible(true); container.requestFocusInWindow(); } } }
public static void centerDialog(final Window dialog) { final GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); final Rectangle screenBounds = config.getBounds(); final Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(config); screenBounds.x = screenBounds.x + insets.left; screenBounds.y = screenBounds.y + insets.top; screenBounds.width = screenBounds.width - (insets.left + insets.right); screenBounds.height = screenBounds.height - (insets.top + insets.bottom); // remember that casting doubles to int's always rounds down. dialog.setLocation( ((screenBounds.width / 2) + screenBounds.x - (dialog.getWidth() / 2)), ((screenBounds.height / 2) + screenBounds.y - (dialog.getHeight() / 2))); }
public static void main(String[] args) { final Window window = new Window(g_szApplicationName); window.setIconImage(LoadIcon("/SimpleSoccer/icon1.png")); window.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); buffer = new BufferedImage(WindowWidth, WindowHeight, BufferedImage.TYPE_INT_RGB); hdcBackBuffer = buffer.createGraphics(); // these hold the dimensions of the client window area cxClient = buffer.getWidth(); cyClient = buffer.getHeight(); // seed random number generator common.misc.utils.setSeed(0); window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); // Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); window.setResizable(false); int y = center.y - window.getHeight() / 2; window.setLocation(center.x - window.getWidth() / 2, y >= 0 ? y : 0); Script1.MyMenuBar menu = Script1.createMenu(IDR_MENU1); window.setJMenuBar(menu); g_SoccerPitch = new SoccerPitch(cxClient, cyClient); CheckAllMenuItemsAppropriately(menu); createPanel(); window.add(panel); window.pack(); window.addKeyListener( new KeyAdapter() { @Override public void keyReleased(KeyEvent e) { CppToJava.keyCache.released(e); switch (e.getKeyChar()) { case KeyEvent.VK_ESCAPE: { System.exit(0); } break; case 'r': case 'R': { SoccerPitchLock.lock(); g_SoccerPitch = null; g_SoccerPitch = new SoccerPitch(cxClient, cyClient); JMenuBar bar = Script1.createMenu(IDR_MENU1); window.setJMenuBar(bar); bar.revalidate(); SoccerPitchLock.unlock(); } break; case 'p': case 'P': { g_SoccerPitch.TogglePause(); } break; } // end switch } // end switch } @Override public void keyPressed(KeyEvent e) { CppToJava.keyCache.pressed(e); } }); window.addComponentListener( new ComponentAdapter() { @Override // has the user resized the client area? public void componentResized(ComponentEvent e) { // if so we need to update our variables so that any drawing // we do using cxClient and cyClient is scaled accordingly cxClient = e.getComponent().getBounds().width; cyClient = e.getComponent().getBounds().height; // now to resize the backbuffer accordingly. buffer = new BufferedImage(cxClient, cyClient, BufferedImage.TYPE_INT_RGB); hdcBackBuffer = buffer.createGraphics(); } }); // make the window visible window.setVisible(true); // timer.SmoothUpdatesOn(); // start the timer timer.Start(); while (true) { // update if (timer.ReadyForNextFrame()) { SoccerPitchLock.lock(); g_SoccerPitch.Update(); SoccerPitchLock.unlock(); // render // panel.revalidate(); panel.repaint(); try { // System.out.println(timer.TimeElapsed()); Thread.sleep(2); } catch (InterruptedException ex) { } } } // end while }
public static void centreWindow(Window frame) { Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2); int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2); frame.setLocation(x, y); }
/** Installs the sheet on the owner. This method is invoked just before the JSheet is shown. */ protected void installSheet() { if (!isNativeSheetSupported() && !isInstalled && isExperimentalSheet()) { Window owner = getOwner(); if (owner != null) { owner.addWindowListener(windowEventHandler); } isInstalled = true; } else { Window owner = getOwner(); if (owner != null) { // Determine the location for the sheet and its owner while // the sheet will be visible. // In case we have to shift the owner to fully display the // dialog, we remember the shift back position. Point ownerLoc = owner.getLocation(); Point sheetLoc; if (isShowAsSheet()) { if (owner instanceof JFrame) { sheetLoc = new Point( ownerLoc.x + (owner.getWidth() - getWidth()) / 2, ownerLoc.y + owner.getInsets().top + ((JFrame) owner).getRootPane().getContentPane().getY()); } else if (owner instanceof JDialog) { sheetLoc = new Point( ownerLoc.x + (owner.getWidth() - getWidth()) / 2, ownerLoc.y + owner.getInsets().top + ((JDialog) owner).getRootPane().getContentPane().getY()); } else { sheetLoc = new Point( ownerLoc.x + (owner.getWidth() - getWidth()) / 2, ownerLoc.y + owner.getInsets().top); } if (sheetLoc.x < 0) { owner.setLocation(ownerLoc.x - sheetLoc.x, ownerLoc.y); sheetLoc.x = 0; shiftBackLocation = ownerLoc; oldLocation = owner.getLocation(); } else { shiftBackLocation = null; oldLocation = ownerLoc; } } else { sheetLoc = new Point( ownerLoc.x + (owner.getWidth() - getWidth()) / 2, ownerLoc.y + (owner.getHeight() - getHeight()) / 2); } setLocation(sheetLoc); oldFocusOwner = owner.getFocusOwner(); // Note: We mustn't change the windows focusable state because // this also affects the focusable state of the JSheet. // owner.setFocusableWindowState(false); owner.setEnabled(false); // ((JFrame) owner).setResizable(false); if (isShowAsSheet()) { owner.addComponentListener(ownerMovementHandler); } else { if (owner instanceof Frame) { setTitle(((Frame) owner).getTitle()); } } } isInstalled = true; } }
public static void setToplevelLocation( Window toplevel, Component component, int relativePosition) { Rectangle compBounds = component.getBounds(); // Convert component location to screen coordinates Point p = new Point(); SwingUtilities.convertPointToScreen(p, component); int x; int y; // Set frame location to be centered on panel switch (relativePosition) { case SwingConstants.NORTH: { x = (p.x + (compBounds.width / 2)) - (toplevel.getWidth() / 2); y = p.y - toplevel.getHeight(); break; } case SwingConstants.EAST: { x = p.x + compBounds.width; y = (p.y + (compBounds.height / 2)) - (toplevel.getHeight() / 2); break; } case SwingConstants.SOUTH: { x = (p.x + (compBounds.width / 2)) - (toplevel.getWidth() / 2); y = p.y + compBounds.height; break; } case SwingConstants.WEST: { x = p.x - toplevel.getWidth(); y = (p.y + (compBounds.height / 2)) - (toplevel.getHeight() / 2); break; } case SwingConstants.NORTH_EAST: { x = p.x + compBounds.width; y = p.y - toplevel.getHeight(); break; } case SwingConstants.NORTH_WEST: { x = p.x - toplevel.getWidth(); y = p.y - toplevel.getHeight(); break; } case SwingConstants.SOUTH_EAST: { x = p.x + compBounds.width; y = p.y + compBounds.height; break; } case SwingConstants.SOUTH_WEST: { x = p.x - toplevel.getWidth(); y = p.y + compBounds.height; break; } default: case SwingConstants.CENTER: { x = (p.x + (compBounds.width / 2)) - (toplevel.getWidth() / 2); y = (p.y + (compBounds.height / 2)) - (toplevel.getHeight() / 2); } } toplevel.setLocation(x, y); }
/** Centre a Window on the screen */ public static void centre(Window w) { Dimension us = w.getSize(), them = Toolkit.getDefaultToolkit().getScreenSize(); int newX = (them.width - us.width) / 2; int newY = (them.height - us.height) / 2; w.setLocation(newX, newY); }
public void mouseDragged(MouseEvent ev) { Window w = (Window) ev.getSource(); Point pt = ev.getPoint(); if (isMovingWindow) { Point windowPt; try { windowPt = (Point) AccessController.doPrivileged(getLocationAction); windowPt.x = windowPt.x - dragOffsetX; windowPt.y = windowPt.y - dragOffsetY; w.setLocation(windowPt); } catch (PrivilegedActionException e) { } } else if (dragCursor != 0) { Rectangle r = w.getBounds(); Rectangle startBounds = new Rectangle(r); Dimension min = w.getMinimumSize(); switch (dragCursor) { case Cursor.E_RESIZE_CURSOR: adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, 0); break; case Cursor.S_RESIZE_CURSOR: adjust(r, min, 0, 0, 0, pt.y + (dragHeight - dragOffsetY) - r.height); break; case Cursor.N_RESIZE_CURSOR: adjust(r, min, 0, pt.y - dragOffsetY, 0, -(pt.y - dragOffsetY)); break; case Cursor.W_RESIZE_CURSOR: adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), 0); break; case Cursor.NE_RESIZE_CURSOR: adjust( r, min, 0, pt.y - dragOffsetY, pt.x + (dragWidth - dragOffsetX) - r.width, -(pt.y - dragOffsetY)); break; case Cursor.SE_RESIZE_CURSOR: adjust( r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, pt.y + (dragHeight - dragOffsetY) - r.height); break; case Cursor.NW_RESIZE_CURSOR: adjust( r, min, pt.x - dragOffsetX, pt.y - dragOffsetY, -(pt.x - dragOffsetX), -(pt.y - dragOffsetY)); break; case Cursor.SW_RESIZE_CURSOR: adjust( r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), pt.y + (dragHeight - dragOffsetY) - r.height); break; default: break; } if (!r.equals(startBounds)) { w.setBounds(r); // Defer repaint/validate on mouseReleased unless dynamic // layout is active. if (Toolkit.getDefaultToolkit().isDynamicLayoutActive()) { w.validate(); getRootPane().repaint(); } } } }
public static void center(Window w) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension windowSize = w.getSize(); w.setLocation( (screenSize.width - windowSize.width) / 2, (screenSize.height - windowSize.height) / 2); }