JDialog is specify that pressing the Escape key cancels the dialog. : JDialog « Swing « Java Tutorial






JDialog is specify that pressing the Escape key cancels the dialog.
import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

class EscapeDialog extends JDialog {
  public EscapeDialog() {
    super((Frame) null, false);
  }

  protected JRootPane createRootPane() {
    JRootPane rootPane = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action actionListener = new AbstractAction() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("about to disappear");
        setVisible(false);
      }
    };
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPane.getActionMap().put("ESCAPE", actionListener);

    return rootPane;
  }

}

public class EscapeDialogTest {
  public static void main(String[] a) {
    EscapeDialog dlg = new EscapeDialog();
    dlg.add(new JButton("asdf"));
    dlg.setSize(300, 100);
    dlg.setVisible(true);
  }
}








14.74.JDialog
14.74.1.Dialog Introduction
14.74.2.A Simple Modal DialogA Simple Modal Dialog
14.74.3.JDialog is specify that pressing the Escape key cancels the dialog.JDialog is specify that pressing the Escape key cancels the dialog.
14.74.4.Address DialogAddress Dialog
14.74.5.Set Default Close Operation for Dialog
14.74.6.Extending JDialog
14.74.7.extends JDialog to create your own dialog
14.74.8.The base class for standard dialogs.
14.74.9.A dialog that presents the user with a sequence of steps for completing a task.
14.74.10.Positions the specified dialog at a position relative to its parent.
14.74.11.A dialog allow selection and a font and its associated info.
14.74.12.Font Chooser extends javax.swing.JDialog
14.74.13.FontChooser by Noah w
14.74.14.Dialog Panel