下面列出了javax.swing.JRootPane#setDefaultButton ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Change the default button of the root pane of the specified
* component.
*
* @param component the component whose root pane's default button
* should be changed. It must be a JButton.
* @param setDefault if <code>true</code>, set the JButton specified by
* <code>component</code> as the default. Otherwise the default button
* is set to none
*/
private void changeDefault(Component component, boolean setDefault) {
if (component instanceof JButton) {
JButton button = (JButton) component;
JRootPane parent = button.getRootPane();
/*
* In some conditions, when pushing the button results
* in it being removed from the root container, a focus
* event can be processed after the button (or its parents)
* has been removed, so we need to check for a non-null
* root for a focused button, even though that seems
* nonsensical.
*/
if (parent != null) {
if (setDefault) {
parent.setDefaultButton(button);
} else {
parent.setDefaultButton(null);
}
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void selectInitialValue(JOptionPane op) {
if (initialFocusComponent != null) {
initialFocusComponent.requestFocus();
if (initialFocusComponent instanceof JButton) {
JRootPane root = SwingUtilities.getRootPane(initialFocusComponent);
if (root != null) {
root.setDefaultButton((JButton)initialFocusComponent);
}
}
}
}
public DefaultRegistry(JRootPane rootPane) {
this.rootPane = rootPane;
rootPane.setDefaultButton(null);
}