public void centerDialog(Dialog dialog) { // For centering dialogs Dimension size = dialog.getSize(); Dimension parentSize = getSize(); Point parentLocation = getLocationOnScreen(); if ((parentSize.width <= size.width) || (parentSize.height <= size.height)) // If this window is bigger than the parent window, // place it at the same coordinates as the parent. dialog.setLocation(parentLocation.x, parentLocation.y); else // Otherwise, place it centered within the parent window. dialog.setLocation( (((parentSize.width - size.width) / 2) + parentLocation.x), (((parentSize.height - size.height) / 2) + parentLocation.y)); }
protected synchronized Dialog createGotoDialog() { if (gotoDialog == null) { gotoDialog = DialogSupport.createDialog( NbBundle.getBundle(org.netbeans.editor.BaseKit.class) .getString("goto-title"), // NOI18N gotoPanel, false, // non-modal gotoButtons, false, // sidebuttons, 0, // defaultIndex = 0 => gotoButton 1, // cancelIndex = 1 => cancelButton this // listener ); gotoDialog.pack(); // Position the dialog according to the history Rectangle lastBounds = (Rectangle) EditorState.get(BOUNDS_KEY); if (lastBounds != null) { gotoDialog.setBounds(lastBounds); } else { // no history, center it on the screen Dimension dim = gotoDialog.getPreferredSize(); int x; int y; JTextComponent c = EditorRegistry.lastFocusedComponent(); Window w = c != null ? SwingUtilities.getWindowAncestor(c) : null; if (w != null) { x = Math.max(0, w.getX() + (w.getWidth() - dim.width) / 2); y = Math.max(0, w.getY() + (w.getHeight() - dim.height) / 2); } else { Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); x = Math.max(0, (screen.width - dim.width) / 2); y = Math.max(0, (screen.height - dim.height) / 2); } gotoDialog.setLocation(x, y); } return gotoDialog; } else { gotoDialog.setVisible(true); gotoDialog.toFront(); return null; } }
private void openDialog(String title, String string) { // TODO Auto-generated method stub Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension dialogSize = new Dimension(200, 120); final Dialog dialog = new Dialog((Frame) getParent(), title); dialog.setSize(dialogSize); dialog.setLayout(null); dialog.setResizable(true); Label label = new Label(string); label.setSize(400, 50); label.setLocation(20, 30); dialog.add(label, "Center"); Button enter = new Button("Return"); enter.setSize(50, 25); enter.setLocation(75, 80); enter.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub dialog.dispose(); } }); dialog.add(enter); dialog.setLocation( (screenSize.width - dialogSize.width + 50) / 2, (screenSize.height - dialogSize.height + 40) / 2); dialog.setModal(true); dialog.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent arg0) { // TODO Auto-generated method stub dialog.dispose(); } }); dialog.setVisible(true); }
public static void main(String[] args) { Common.initRobot(); final JButton start = new JButton("start"); start.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { act0(); Common.robot.delay(500); act1(); Common.robot.delay(1000); act2(); // Common.robot.delay(500); // act3(); // Common.robot.delay(500); // act4(); // Common.robot.delay(500); // act5(); // Common.robot.delay(500); // act6(); // Common.robot.delay(500); // act7(); start.setText("stopped"); } }); dashBoard.setLayout(new FlowLayout()); dashBoard.add(start); dashBoard.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); dashBoard.setLocation(new Point(10, 650)); dashBoard.setSize(300, 70); dashBoard.setAlwaysOnTop(true); dashBoard.setVisible(true); }
public void makeTheWarning( int X, int Y, int width, int height, Color fg, Color bg, String title, String text, boolean oneLine, Frame frame) { Font warnFont = new java.awt.Font("SanSerif", Font.BOLD, 12); FontMetrics warnFontMetrics = getFontMetrics(warnFont); // Create Dialog window with modal blocking set to true. // Make final so inner class below can access it. final Dialog mww = new Dialog(frame, title, true); mww.setLayout(new BorderLayout()); mww.setSize(width, height); mww.setLocation(X, Y); // Use Label for 1-line warning if (oneLine) { Label hT = new Label(text, Label.CENTER); hT.setForeground(fg); hT.setBackground(bg); hT.setFont(warnFont); mww.add("Center", hT); // Use TextArea for multiline warning } else { TextArea hT = new TextArea("", height, width, TextArea.SCROLLBARS_NONE); hT.setEditable(false); hT.setForeground(fg); hT.setBackground(bg); // no effect once setEditable (false)? hT.setFont(warnFont); mww.add("Center", hT); hT.appendText(text); } mww.setTitle(title); // Add dismiss button Panel botPanel = new Panel(); botPanel.setBackground(Color.lightGray); Label label1 = new Label(); Label label2 = new Label(); Button dismissButton = new Button("Dismiss"); botPanel.add(label1); botPanel.add(dismissButton); botPanel.add(label2); // Add inner class event handler for Dismiss button. This must be // added to the dismissButton before botPanel is added to mww. dismissButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { mww.hide(); mww.dispose(); } }); mww.add("South", botPanel); // Add window closing button (inner class) mww.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { mww.hide(); mww.dispose(); } }); mww.show(); // Note that this show must come after all the above // additions; otherwise they are not added before the // window is displayed. }
/* Display the dialog at the specified location. */ public void setLocation(int x, int y) { super.setLocation(x, y); centerDialog = false; }