/** * Overrides <code>JComponent.removeNotify</code> to check if this button is currently set as the * default button on the <code>RootPane</code>, and if so, sets the <code>RootPane</code>'s * default button to <code>null</code> to ensure the <code>RootPane</code> doesn't hold onto an * invalid button reference. */ public void removeNotify() { JRootPane root = SwingUtilities.getRootPane(this); if (root != null && root.getDefaultButton() == this) { root.setDefaultButton(null); } super.removeNotify(); }
/** * If inputComponent is non-null, the focus is requested on that, otherwise request focus on the * default value */ public void selectInitialValue(JOptionPane op) { if (inputComponent != null) inputComponent.requestFocus(); else { if (initialFocusComponent != null) initialFocusComponent.requestFocus(); if (initialFocusComponent instanceof JButton) { JRootPane root = SwingUtilities.getRootPane(initialFocusComponent); if (root != null) { root.setDefaultButton((JButton) initialFocusComponent); } } } }
private static JPanel createPanel(JFrame frame) { JPanel panel = new JPanel(); JButton button = new JButton("Button"); JButton button2 = new JButton("Button 2"); JButton button3 = new JButton("Button 3"); JRootPane root = frame.getRootPane(); root.setDefaultButton(button); panel.add(button); panel.add(button2); panel.add(button3); return panel; }