A sample modal dialog that displays a message and waits for the user to click the Ok button : Dialog « Swing JFC « Java






A sample modal dialog that displays a message and waits for the user to click the Ok button

 
/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)

   This program 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.

   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

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

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

/**
 * @version 1.33 2007-06-12
 * @author Cay Horstmann
 */
public class DialogTest
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(new Runnable()
         {
            public void run()
            {
               DialogFrame frame = new DialogFrame();
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
            }
         });
   }
}

/**
 * A frame with a menu whose File->About action shows a dialog.
 */
class DialogFrame extends JFrame
{
   public DialogFrame()
   {
      setTitle("DialogTest");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      // construct a File menu

      JMenuBar menuBar = new JMenuBar();
      setJMenuBar(menuBar);
      JMenu fileMenu = new JMenu("File");
      menuBar.add(fileMenu);

      // add About and Exit menu items

      // The About item shows the About dialog

      JMenuItem aboutItem = new JMenuItem("About");
      aboutItem.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               if (dialog == null) // first time
               dialog = new AboutDialog(DialogFrame.this);
               dialog.setVisible(true); // pop up dialog
            }
         });
      fileMenu.add(aboutItem);

      // The Exit item exits the program

      JMenuItem exitItem = new JMenuItem("Exit");
      exitItem.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               System.exit(0);
            }
         });
      fileMenu.add(exitItem);
   }

   public static final int DEFAULT_WIDTH = 300;
   public static final int DEFAULT_HEIGHT = 200;

   private AboutDialog dialog;
}

/**
 * A sample modal dialog that displays a message and waits for the user to click the Ok button.
 */
class AboutDialog extends JDialog
{
   public AboutDialog(JFrame owner)
   {
      super(owner, "About DialogTest", true);

      // add HTML label to center

      add(
            new JLabel(
                  "<html><h1><i>Core Java</i></h1><hr>By Cay Horstmann and Gary Cornell</html>"),
            BorderLayout.CENTER);

      // Ok button closes the dialog

      JButton ok = new JButton("Ok");
      ok.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               setVisible(false);
            }
         });

      // add Ok button to southern border

      JPanel panel = new JPanel();
      panel.add(ok);
      add(panel, BorderLayout.SOUTH);

      setSize(250, 150);
   }
}

   
  








Related examples in the same category

1.Creating and using Dialog BoxesCreating and using Dialog Boxes
2.Dialog boxes and creating your own componentsDialog boxes and creating your own components
3.A frame that can easily support internal frame dialogsA frame that can easily support internal frame dialogs
4.An example of using the JOptionPane with a custom list of options in anAn example of using the JOptionPane with a custom list of options in an
5.See the differences between various types of option panesSee the differences between various types of option panes
6.Vote DialogVote Dialog
7.Create simple about dialogCreate simple about dialog
8.Dialog separatorDialog separator
9.Message dialogMessage dialog
10.Error message dialogError message dialog
11.Information dialog with customized logoInformation dialog with customized logo
12.Input dialog with user-defined logoInput dialog with user-defined logo
13.Confirmation dialogConfirmation dialog
14.Default button for dialog: press Enter to activateDefault button for dialog: press Enter to activate
15.Simple dialog for asking a yes no questionSimple dialog for asking a yes no question
16.Class to Prompt the User for an ID and Password
17.Simple Save Dialog demoSimple Save Dialog demo
18.Demonstrate JOptionPaneDemonstrate JOptionPane
19.Create Color Sample PopupCreate Color Sample Popup
20.Simple Input DialogSimple Input Dialog
21.No button dialogNo button dialog
22.Message Dialog demo Message Dialog demo
23.Escape Key close Dialog
24.Dialog can be closed by pressing the escape key
25.Dialog which displays indeterminate progress
26.Dialog with Escape KeyDialog with Escape Key
27.Modal Message Dialog
28.A frame with a menu whose File->Connect action shows a password dialogA frame with a menu whose File->Connect action shows a password dialog