Java Utililty Methods JRootPane

List of utility methods to do JRootPane

Description

The list of methods to do JRootPane are organized into topic(s).

Method

voidaddShortcut(JRootPane rootPane, String command, Action action)
add Shortcut
KeyStroke stroke = KeyStroke.getKeyStroke(command);
addShortcut(rootPane, command, action, stroke);
voidcloseOnEscape(final Window window, final JRootPane root)
Make the window close when the used presses escape.
InputMap map = root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
map.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), WINDOW_CLOSE);
Action dispatchClosing = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent event) {
        window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
};
...
JRootPanegetRootPane(Window window)
get Root Pane
if (window instanceof JFrame) {
    return ((JFrame) window).getRootPane();
if (window instanceof JDialog) {
    return ((JDialog) window).getRootPane();
return null;
voidinstallEscapeBinding(final Window window, final JRootPane rootPane, final boolean dispose)
install Escape Binding
final KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
final InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(stroke, "ESCAPE");
rootPane.getActionMap().put("ESCAPE", new AbstractAction() {
    private static final long serialVersionUID = 1L;
    @Override
    public void actionPerformed(ActionEvent e) {
        if (dispose) {
...
booleanisSecondaryWindow(JRootPane rp)
is Secondary Window
Component c = rp.getParent();
if (c instanceof JInternalFrame) {
    return true;
} else {
    return false;
voidmakeWindowLeopardStyle(JRootPane rootPane)
Makes this window a Unified window on Mac OS X Leopard or greater systems.
if (rootPane.isValid()) {
    throw new IllegalArgumentException(
            "This method only works if the" + "given JRootPane has not yet been realized.");
rootPane.putClientProperty("apple.awt.brushMetalLook", Boolean.TRUE);
voidregisterEscapeAction(JRootPane pane, ActionListener l)
register key action when VK_ESCAPE pressed
KeyStroke keyStroke = KeyStroke.getKeyStroke((char) KeyEvent.VK_ESCAPE);
pane.registerKeyboardAction(l, keyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
voidsetCancelAction(RootPaneContainer c, Action cancelAction)
Set up the escape keyboard shortcut for a JFrame or JDialog.
((JPanel) c.getContentPane()).getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
        .put(KeyStroke.getKeyStroke("ESCAPE"), "cancelAction");
((JPanel) c.getContentPane()).getActionMap().put("cancelAction", cancelAction);
voidsetEscapeAction(JRootPane pane, Action action)
Associate a custom action to be called when the Esc key is pressed.
pane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ESC_KEYSTROKE, "ESCAPE");
pane.getActionMap().put("ESCAPE", action);