//Import swing classes for building the GUI
import javax.swing.*;
//import AWT's event-handling classes
import java.awt.event.*;
//We also need to import the specific layout manager classes (we'll just
//include everything in the awt package here...)
import java.awt.*;


//Extend the JDialog class to make custon dialogs
public class MyDialog extends JDialog implements ActionListener {
  //Variables representing the possible options taken by the user: press
  //the "OK" or "Cancel" button (or close the window.. just use CANCEL_OPTION)
  public static final int OK_OPTION = 0;
  public static final int CANCEL_OPTION = 1;

  //The option the user selected
  private int userAction = CANCEL_OPTION;

  //Swing GUI components
  JPanel main, buttonPanel;
  JButton ok, cancel;
  JTextField text;


  //The constructor creates the GUI for the dialog
  public MyDialog() {
    setTitle("A Custom Dialog");

    //Create the main panel with a grid layout
    main = new JPanel(new GridLayout(2,1));
    this.getContentPane().add(main);

    //Create the text field and add it to the panel
    text = new JTextField();
    main.add(text);

    //Create the button panel and buttons.  Default layout is FlowLayout
    buttonPanel = new JPanel();
    //Set ActionListeners for the buttons as you create them
    ok = new JButton("OK");
    ok.addActionListener(this);
    cancel = new JButton("Cancel");
    cancel.addActionListener(this);
    //Add them to the button panel
    buttonPanel.add(ok);
    buttonPanel.add(cancel);

    //Finally, add the buttonPanel to the main panel
    main.add(buttonPanel);

    //Don't allow the window to be resized
    setResizable(false);
    //Set the dialog to be modal, so that interaction with any other part of
    //the GUI is not allowed until the dialog is somehow closed
    setModal(true);
    //Pack all the components together
    pack();
    //Don't show the dialog... we can show it later, after it's created,
    //by calling showMyDialog
  }


  //The actionPerformed() method catches button presses
  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == ok) {
      //Record the user action and close the window
      userAction = OK_OPTION;
      this.dispose();
    }
    else if(e.getSource() == cancel) {
      //Record the user action and close the window
      userAction = CANCEL_OPTION;
      this.dispose();
    }
  }


  //Get the
  public int getUserAction() {
    return userAction;
  }

  //Getter function for user input into the text field
  public String getTextFieldContents() {
    return text.getText();
  }

  /**
 * This method show()'s the dialog, thus preventing the user
 * from accessing the parent GUI until the dialog box is filled out and
 * submitted, or is closed.  Returns a value indicating if the dialog was
 * submitted or closed (canceled).
 */
public int showMyDialog() {
  this.show();
  return userAction;
}


//Create a GUI with a button to press to bring up the dialog
public static void main(String[] args) {
  JFrame frame = new JFrame("Test MyDialog");
  JButton button = new JButton("Press me!");

  //Create a button with an anonymous ActionListener inner class.  This
  //button will pop up a MyDialog instance
  button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      //Create and show the dialog when the button is pressed
      MyDialog dialog = new MyDialog();
      int value = dialog.showMyDialog();

      //The rest of the code won't be executed until the dialog is closed:
      //Tell which button the user pressed
      if(value == MyDialog.OK_OPTION)
        System.out.println("User pressed OK");
      else if(value == MyDialog.CANCEL_OPTION)
        System.out.println("User pressed Cancel");
      //Print the contents of the text field to the console
      System.out.println("User typed: " + dialog.getTextFieldContents());
      System.out.println();
    }
  });

  //Add the button to the frame
  frame.getContentPane().add(button);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.show();
}


}