Java JFrame newModalDialog(JFrame owner, String title, JPanel content, JButton... buttons)

Here you can find the source of newModalDialog(JFrame owner, String title, JPanel content, JButton... buttons)

Description

Builds a standard modal input dialog, with content and buttons to accept or cancel that content.

License

Apache License

Declaration

public static JDialog newModalDialog(JFrame owner, String title, JPanel content, JButton... buttons) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import javax.swing.Action;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
    /**/*from   w  w  w .  j  ava 2  s . com*/
     *  Builds a standard modal input dialog, with content and buttons to
     *  accept or cancel that content.
     */
    public static JDialog newModalDialog(JFrame owner, String title, JPanel content, JButton... buttons) {
        java.awt.GridBagConstraints gridBagConstraints;

        int i = 0;
        for (JButton b : buttons) {
            i++;
            b.setPreferredSize(new java.awt.Dimension(100, 24));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = (i + 2);
            gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
            gridBagConstraints.weighty = 1.0;
            gridBagConstraints.insets = new java.awt.Insets(3, 10, 3, 10);
            content.add(b, gridBagConstraints);
        }

        //        
        //        
        //        JPanel buttonPanel = new JPanel();
        //        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
        //        buttonPanel.setAlignmentX(0.0f);
        //        buttonPanel.add(Box.createHorizontalGlue());
        //        for (int ii = 0 ; ii < buttons.length ; ii++)
        //        {
        //            if (ii > 0)
        //                buttonPanel.add(interButtonSpace());
        //            buttonPanel.add(buttons[ii]);
        //        }
        //
        //        JPanel panel = new JPanel();
        //        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        //        panel.setBorder(dialogBorder());
        //        panel.add(content);
        //        panel.add(Box.createVerticalStrut(18));
        //        panel.add(buttonPanel);
        //
        JDialog theDialog = new JDialog(owner, title, true);
        theDialog.setContentPane(content);
        theDialog.pack();
        return theDialog;
    }

    /**
     *  Builds a standard modal input dialog, with content and buttons to
     *  accept or cancel that content.
     */
    public static JDialog newModalDialog(JFrame owner, String title, JPanel content, Action... actions) {
        JButton[] buttons = new JButton[actions.length];
        for (int ii = 0; ii < actions.length; ii++) {
            buttons[ii] = new JButton(actions[ii]);
        }
        return newModalDialog(owner, title, content, buttons);
    }
}

Related

  1. minimize(JFrame frame)
  2. msgBox(JFrame frame, String title, String message)
  3. newDialog(JFrame owner, String title, JPanel content)
  4. newExitActionListener(final JFrame jFrame)
  5. newJFrame(Component c, String title, int w, int h)
  6. optionDialog(String title, String text, String option1, String option2, int defaultOption, JFrame parentFrame)
  7. packFrame(JFrame frame)
  8. packJFrameWindow(final JComponent comp)
  9. POPUP(JFrame frame, String message)