protected void center(JDialog dialog) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Point center = ge.getCenterPoint(); int w = dialog.getPreferredSize().width; int h = dialog.getPreferredSize().height; int x = center.x - w / 2, y = center.y - h / 2; dialog.setBounds(x, y, w, h); dialog.validate(); }
public void runCalculatorDialog(MouseEvent e) { if (calculatorDialog == null) { JFrame frame = null; Container container = getTopLevelAncestor(); if (container instanceof JFrame) { frame = (JFrame) container; } calculatorDialog = new JDialog(frame, "Rechner", true); calculator = new Calculator(); calculator.getOKButton().addActionListener(this); calculatorDialog.getContentPane().add(calculator); calculatorDialog.pack(); } // Set calculator's init value Money money = getValue(); if (money != null) { calculator.setValue(money.getValue()); } else { calculator.setValue(0.0); } // calculate POP (point of presentation) Point point = ((JComponent) e.getSource()).getLocationOnScreen(); int x = point.x + e.getX(); int y = point.y + e.getY(); // ensure that it does not exceed the screen limits Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); Dimension dim = calculatorDialog.getPreferredSize(); if (x + dim.width >= screenDim.width) { x = screenDim.width - dim.width - 1; } if (y + dim.height >= screenDim.height) { y = screenDim.height - dim.height - 1; } // make it visible at wanted location calculatorDialog.setLocation(x, y); calculatorDialog.show(); }
public void setCenterDialog(JDialog d) { d.setLocation( (Toolkit.getDefaultToolkit().getScreenSize().width - d.getPreferredSize().width) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - d.getPreferredSize().height) / 2); }