Java JFrame Parent showDialog(Frame parent, String title, JComponent c)

Here you can find the source of showDialog(Frame parent, String title, JComponent c)

Description

Show the dialog with OK, Cancel buttons.

License

Open Source License

Parameter

Parameter Description
parent Parent JFrame to locate dialog near.
title Dialog box title.
c The JComponent that is the main UI.

Return

True if the User hit "OK".

Declaration

public static boolean showDialog(Frame parent, String title, JComponent c) 

Method Source Code

//package com.java2s;
/*/*from w w  w  .jav a  2  s .c o m*/
This file is part of JFLICKS.
    
JFLICKS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
JFLICKS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with JFLICKS.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.Frame;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;

import javax.swing.JSeparator;

public class Main {
    /**
     * Show the dialog with OK, Cancel buttons.
     *
     * @param parent Parent JFrame to locate dialog near.
     * @param title Dialog box title.
     * @param c The JComponent that is the main UI.
     * @return True if the User hit "OK".
     */
    public static boolean showDialog(Frame parent, String title, JComponent c) {
        return (showDialog(parent, title, c, true));
    }

    /**
     * Show the dialog with optional Cancel button.
     *
     * @param parent Parent JFrame to locate dialog near.
     * @param title Dialog box title.
     * @param c The JComponent that is the main UI.
     * @param includeCancel False if you want a simple OK dialog.
     * @return True if the User hit "OK".
     */
    public static boolean showDialog(Frame parent, String title, JComponent c, boolean includeCancel) {

        boolean result = false;
        final JButton ok = new JButton("OK");
        final JButton cancel = new JButton("Cancel");
        final JDialog dialog = new JDialog(parent, title, true);
        ok.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {

                dialog.setVisible(false);
                ok.setText("true");
                dialog.dispose();
            }
        });
        cancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {

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

        dialog.getContentPane().setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.gridwidth = 2;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(4, 4, 4, 4);

        dialog.getContentPane().add(c, gbc);

        gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.weighty = 0.0;
        gbc.gridwidth = 2;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(4, 4, 4, 4);

        dialog.getContentPane().add(new JSeparator(), gbc);

        gbc = new GridBagConstraints();
        gbc.weightx = 0.5;
        gbc.weighty = 0.0;
        gbc.gridwidth = 1;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.fill = GridBagConstraints.NONE;
        gbc.insets = new Insets(4, 4, 4, 4);

        dialog.getContentPane().add(ok, gbc);

        if (includeCancel) {

            gbc = new GridBagConstraints();
            gbc.weightx = 0.5;
            gbc.weighty = 0.0;
            gbc.gridwidth = 1;
            gbc.gridx = 1;
            gbc.gridy = 2;
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.fill = GridBagConstraints.NONE;
            gbc.insets = new Insets(4, 4, 4, 4);

            dialog.getContentPane().add(cancel, gbc);
        }

        dialog.pack();

        dialog.setLocationRelativeTo(parent);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
        result = ok.getText().equals("true");

        return (result);
    }
}

Related

  1. oops(Frame parent, String s)
  2. rootFrame(@Nonnull Component component)
  3. runProgressDialog(Runnable runnable, Frame parentFrame, String title, int count)
  4. setFrameParentTitle(Container c, String s)
  5. showDialog(Frame aParent, String aMsg, String aTitle, int aType)
  6. showDoneDialog(Frame parent, String title, JComponent c)
  7. showGeneralErrorDialog(Frame parent, String title, String message)
  8. showInfoDialog(final Frame parent, final String msg)
  9. showNothingIsSelectedForRemoveDialog(Frame parent)