Example usage for javax.swing JDialog dispose

List of usage examples for javax.swing JDialog dispose

Introduction

In this page you can find the example usage for javax.swing JDialog dispose.

Prototype

public void dispose() 

Source Link

Document

Releases all of the native screen resources used by this Window , its subcomponents, and all of its owned children.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("Frame");
    frame.add(Box.createRigidArea(new Dimension(400, 300)));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();// w ww.java 2 s .c  om
    frame.setVisible(true);

    JDialog dialog = new JDialog(frame, "Dialog", true);

    int condition = JPanel.WHEN_IN_FOCUSED_WINDOW;
    InputMap inputMap = ((JPanel) dialog.getContentPane()).getInputMap(condition);
    ActionMap actionMap = ((JPanel) dialog.getContentPane()).getActionMap();
    String enter = "enter";
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), enter);
    actionMap.put(enter, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
        }
    });

    dialog.add(Box.createRigidArea(new Dimension(200, 200)));
    dialog.pack();
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);

}

From source file:Main.java

/**
 * Adds a key listener to a given {@link JDialog} that diposes it when the escape
 * key is pressed./* w  ww. j a v a 2 s .c o m*/
 */
public static void addEscapeKeyCloseAction(final JDialog dialog) {
    dialog.getRootPane().registerKeyboardAction(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
}

From source file:Main.java

public static void closeOnEscape(final JDialog parent) {
    parent.getRootPane().getActionMap().put("close_on_escape", new AbstractAction() {
        @Override/*from   w w  w. ja v  a  2s .c  o  m*/
        public void actionPerformed(ActionEvent e) {
            parent.dispose();
        }
    });
    parent.getRootPane().getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close_on_escape");
}

From source file:net.menthor.editor.v2.util.Util.java

/** Helper method for constructing an always-on-top modal dialog. */
public static Object show(String title, int type, Object message, Object[] options, Object initialOption) {
    if (options == null) {
        options = new Object[] { "Ok" };
        initialOption = "Ok";
    }//from w  w w .  j a  v a2  s.c  o  m
    JOptionPane p = new JOptionPane(message, type, JOptionPane.DEFAULT_OPTION, null, options, initialOption);
    p.setInitialValue(initialOption);
    JDialog d = p.createDialog(null, title);
    p.selectInitialValue();
    d.setAlwaysOnTop(true);
    d.setVisible(true);
    d.dispose();
    return p.getValue();
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 1 to 0.
 *///  w  ww  .j  ava 2 s  .c om
public static void fadeOut(final JDialog dialog) {
    final Timer timer = new Timer(10, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 1;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity -= 0.15f;
            dialog.setOpacity(Math.max(opacity, 0));
            if (opacity <= 0) {
                timer.stop();
                dialog.dispose();
            }
        }
    });

    dialog.setOpacity(1);
    timer.start();
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 1 to 0.
 *//*from  w  ww  . j  ava 2s .  c  om*/
public static void fadeOut(final JDialog dialog) {
    final Timer timer = new Timer(10, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {

        private float opacity = 1;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity -= 0.15f;
            dialog.setOpacity(Math.max(opacity, 0));
            if (opacity <= 0) {
                timer.stop();
                dialog.dispose();
            }
        }
    });

    dialog.setOpacity(1);
    timer.start();
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 1 to 0, and then
 * dispose./*from  w w  w .ja v  a2  s  .c om*/
 *
 * @param dialog the dialog to fade out
 * @param delay the delay in ms before starting and between each change
 * @param incrementSize the increment size
 */
public static void fadeOut(final JDialog dialog, int delay, final float incrementSize) {
    final Timer timer = new Timer(delay, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 1;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity -= incrementSize;
            dialog.setOpacity(Math.max(opacity, 0)); // requires java 1.7
            if (opacity < 0) {
                timer.stop();
                dialog.dispose();
            }
        }
    });

    dialog.setOpacity(1); // requires java 1.7
    timer.start();
}

From source file:edu.umass.cs.gnsserver.installer.EC2Runner.java

private static boolean showDialog(String message, final long timeout) {
    try {/*from  w  w w. j  ava  2s  .  c  om*/
        int dialog = JOptionPane.YES_NO_OPTION;
        JOptionPane optionPane = new JOptionPane(message, JOptionPane.QUESTION_MESSAGE,
                JOptionPane.YES_NO_OPTION);
        final JDialog dlg = optionPane.createDialog("Error");
        new Thread(new Runnable() {
            @Override
            public void run() {
                {
                    ThreadUtils.sleep(timeout);
                    dlg.dispose();
                }
            }
        }).start();
        dlg.setVisible(true);
        int value = ((Integer) optionPane.getValue()).intValue();
        if (value == JOptionPane.YES_OPTION) {
            return true;
        } else {
            return false;
        }
    } catch (RuntimeException e) {
        return true;
    }
}

From source file:Main.java

/**
 * Displays the given file chooser. Utility method for avoiding of memory leak in JDK
 * 1.3 {@link javax.swing.JFileChooser#showDialog}.
 *
 * @param chooser the file chooser to display.
 * @param parent the parent window./*  w w  w . j av  a 2 s.c om*/
 * @param approveButtonText the text for the approve button.
 *
 * @return the return code of the chooser.
 */
public static final int showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText) {
    if (approveButtonText != null) {
        chooser.setApproveButtonText(approveButtonText);
        chooser.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG);
    }

    Frame frame = (parent instanceof Frame) ? (Frame) parent
            : (Frame) javax.swing.SwingUtilities.getAncestorOfClass(java.awt.Frame.class, parent);
    String title = chooser.getDialogTitle();

    if (title == null) {
        title = chooser.getUI().getDialogTitle(chooser);
    }

    final JDialog dialog = new JDialog(frame, title, true);
    dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    Container contentPane = dialog.getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(chooser, BorderLayout.CENTER);
    dialog.pack();
    dialog.setLocationRelativeTo(parent);
    chooser.rescanCurrentDirectory();

    final int[] retValue = new int[] { javax.swing.JFileChooser.CANCEL_OPTION };

    ActionListener l = new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            if (ev.getActionCommand() == JFileChooser.APPROVE_SELECTION) {
                retValue[0] = JFileChooser.APPROVE_OPTION;
            }

            dialog.setVisible(false);
            dialog.dispose();
        }
    };

    chooser.addActionListener(l);
    dialog.show();

    return (retValue[0]);
}

From source file:de.codesourcery.jasm16.ide.ui.utils.UIUtils.java

public static void showErrorDialog(Component parent, String title, String textMessage, Throwable cause) {
    final String stacktrace;
    if (cause == null) {
        stacktrace = null;/* w  w  w  .jav a2 s.co m*/
    } else {
        final ByteArrayOutputStream stackTrace = new ByteArrayOutputStream();
        cause.printStackTrace(new PrintStream(stackTrace));
        stacktrace = new String(stackTrace.toByteArray());
    }

    final JDialog dialog = new JDialog((Window) null, title);

    dialog.setModal(true);

    final JTextArea message = createMultiLineLabel(textMessage);
    final DialogResult[] outcome = { DialogResult.CANCEL };

    final JButton okButton = new JButton("Ok");
    okButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            outcome[0] = DialogResult.YES;
            dialog.dispose();
        }
    });

    final JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout());
    buttonPanel.add(okButton);

    final JPanel messagePanel = new JPanel();
    messagePanel.setLayout(new GridBagLayout());

    GridBagConstraints cnstrs = constraints(0, 0, true, true, GridBagConstraints.BOTH);
    cnstrs.weightx = 1;
    cnstrs.weighty = 0.1;
    cnstrs.gridheight = 1;
    messagePanel.add(message, cnstrs);

    if (stacktrace != null) {
        final JTextArea createMultiLineLabel = new JTextArea(stacktrace);

        createMultiLineLabel.setBackground(null);
        createMultiLineLabel.setEditable(false);
        createMultiLineLabel.setBorder(null);
        createMultiLineLabel.setLineWrap(false);
        createMultiLineLabel.setWrapStyleWord(false);

        final JScrollPane pane = new JScrollPane(createMultiLineLabel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        cnstrs = constraints(0, 1, true, true, GridBagConstraints.BOTH);
        cnstrs.weightx = 1.0;
        cnstrs.weighty = 0.9;
        cnstrs.gridwidth = GridBagConstraints.REMAINDER;
        cnstrs.gridheight = GridBagConstraints.REMAINDER;
        messagePanel.add(pane, cnstrs);
    }

    final JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    cnstrs = constraints(0, 0, true, false, GridBagConstraints.BOTH);
    cnstrs.gridwidth = GridBagConstraints.REMAINDER;
    cnstrs.gridheight = 1;
    cnstrs.weighty = 1.0;
    cnstrs.insets = new Insets(5, 2, 5, 2); // top,left,bottom,right           
    panel.add(messagePanel, cnstrs);

    cnstrs = constraints(0, 1, true, true, GridBagConstraints.HORIZONTAL);
    cnstrs.gridwidth = GridBagConstraints.REMAINDER;
    cnstrs.gridheight = 1;
    cnstrs.weighty = 0;
    cnstrs.insets = new Insets(0, 2, 10, 2); // top,left,bottom,right      
    panel.add(buttonPanel, cnstrs);

    dialog.getContentPane().add(panel);

    dialog.setMinimumSize(new Dimension(600, 400));
    dialog.setPreferredSize(new Dimension(600, 400));
    dialog.setMaximumSize(new Dimension(600, 400));

    dialog.pack();
    dialog.setVisible(true);
}