下面列出了javax.swing.JDialog#getRootPane ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Does the work required to make the parameter JDialog be hidden when pressing escape
*
* @param popup
*/
public static void setupHideOnEscape(final JDialog popup)
{
Action aa = new AbstractAction()
{
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent event)
{
popup.setVisible(false);
}
};
final String mapKey = "escapePressed";
JRootPane root = popup.getRootPane();
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeStroke, mapKey);
root.getActionMap().put(mapKey, aa);
}
public static void installEscapeCloseOperation(final JDialog dialog) {
Action dispatchClosing = new AbstractAction() {
public void actionPerformed(ActionEvent event) {
dialog.dispatchEvent(new WindowEvent(
dialog, WindowEvent.WINDOW_CLOSING
));
}
};
JRootPane root = dialog.getRootPane();
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
escapeStroke, dispatchWindowClosingActionMapKey
);
root.getActionMap().put( dispatchWindowClosingActionMapKey, dispatchClosing
);
}
public void open(JComponent progressComponent) {
holder.add(progressComponent, BorderLayout.CENTER);
DialogDescriptor dd = new DialogDescriptor(
this,
NbBundle.getMessage(ProgressPanel.class, "MSG_PleaseWait"),
true,
new Object[0],
DialogDescriptor.NO_OPTION,
DialogDescriptor.DEFAULT_ALIGN,
null,
null,
true);
dialog = DialogDisplayer.getDefault().createDialog(dd);
if (dialog instanceof JDialog) {
JDialog jDialog = ((JDialog)dialog);
jDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
JRootPane rootPane = jDialog.getRootPane();
rootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel"); // NOI18N
rootPane.getActionMap().put("cancel", new AbstractAction() { // NOI18N
public void actionPerformed(ActionEvent event) {
if (cancelButton.isEnabled()) {
cancelButton.doClick();
}
}
});
}
dialog.setResizable(false);
dialog.setVisible(true);
}
public void open(JComponent progressComponent) {
holder.add(progressComponent, BorderLayout.CENTER);
DialogDescriptor dd = new DialogDescriptor(
this,
NbBundle.getMessage(ProgressPanel.class, "MSG_PleaseWait"),
true,
new Object[0],
DialogDescriptor.NO_OPTION,
DialogDescriptor.DEFAULT_ALIGN,
null,
null,
true);
dialog = DialogDisplayer.getDefault().createDialog(dd);
if (dialog instanceof JDialog) {
JDialog jDialog = ((JDialog) dialog);
jDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
JRootPane rootPane = jDialog.getRootPane();
rootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel"); // NOI18N
rootPane.getActionMap().put("cancel", new AbstractAction() { // NOI18N
@Override
public void actionPerformed(ActionEvent event) {
if (cancelButton.isEnabled()) {
cancelButton.doClick();
}
}
});
}
dialog.setResizable(false);
dialog.setVisible(true);
}
public void open(JComponent progressComponent) {
holder.add(progressComponent, BorderLayout.CENTER);
DialogDescriptor dd = new DialogDescriptor(
this,
NbBundle.getMessage (ProgressPanel.class, "MSG_PleaseWait"),
true,
new Object[0],
DialogDescriptor.NO_OPTION,
DialogDescriptor.DEFAULT_ALIGN,
null,
null,
true);
dialog = DialogDisplayer.getDefault().createDialog(dd);
if (dialog instanceof JDialog) {
JDialog jDialog = ((JDialog)dialog);
jDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
JRootPane rootPane = jDialog.getRootPane();
rootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel"); // NOI18N
rootPane.getActionMap().put("cancel", new AbstractAction() { // NOI18N
public void actionPerformed(ActionEvent event) {
if (cancelButton.isEnabled()) {
cancelButton.doClick();
}
}
});
}
dialog.setResizable(false);
dialog.setVisible(true);
}
/**
* Add a keyboard shortcut to allow ESC to close the dialog.
*
* @param dialog The dialog to be updated.
*/
public static void installEscapeCloseOperation(final JDialog dialog)
{
JRootPane root = dialog.getRootPane();
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ESCAPE_STROKE, DISPATCH_WINDOW_CLOSING_ACTION_MAP_KEY);
Action dispatchClosing = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent event)
{
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
}
};
root.getActionMap().put(DISPATCH_WINDOW_CLOSING_ACTION_MAP_KEY, dispatchClosing);
}
public static void installEscapeCloseOperation(final JDialog dialog) {
Action dispatchClosing = new AbstractAction() {
public void actionPerformed(ActionEvent event) {
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
}
};
JRootPane root = dialog.getRootPane();
installCloseAction(dispatchClosing, root);
}
public static void installEscapeCloseOperation(final JDialog dialog) {
@SuppressWarnings("serial")
Action dispatchClosing = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent event) {
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
}
};
JRootPane root = dialog.getRootPane();
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ESCAPE_STROKE, DISPATCH_WINDOW_CLOSING_ACTION_MAP_KEY);
root.getActionMap().put(DISPATCH_WINDOW_CLOSING_ACTION_MAP_KEY, dispatchClosing);
}
/**
* Add a keyboard shortcut to allow ESC to close the dialog.
*
* @param dialog The dialog to be updated.
*/
public static void installEscapeCloseOperation(final JDialog dialog)
{
JRootPane root = dialog.getRootPane();
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ESCAPE_STROKE, DISPATCH_WINDOW_CLOSING_ACTION_MAP_KEY);
Action dispatchClosing = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent event)
{
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
}
};
root.getActionMap().put(DISPATCH_WINDOW_CLOSING_ACTION_MAP_KEY, dispatchClosing);
}
public static void installEscapeCloseOperation(final JDialog dialog) {
Action dispatchClosing = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent event) {
dialog.dispatchEvent(new WindowEvent(
dialog, WindowEvent.WINDOW_CLOSING));
}
};
JRootPane root = dialog.getRootPane();
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
escapeStroke, dispatchWindowClosingActionMapKey);
root.getActionMap().put(dispatchWindowClosingActionMapKey, dispatchClosing);
}