下面列出了javax.swing.JRootPane#getDefaultButton ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static AbstractButton findDefaultButton(Container c, String txt) {
if (c instanceof RootPaneContainer) {
JRootPane root = ((RootPaneContainer) c).getRootPane();
if (root == null) {
return null;
}
AbstractButton btn = root.getDefaultButton();
if (btn == null) {
//Metal L&F does not set default button for JFileChooser
Container parent = c;
while (parent.getParent() != null && !(parent instanceof Dialog)) {
parent = parent.getParent();
}
if (parent instanceof Dialog) {
return findFileChooserAcceptButton ((Dialog) parent, txt);
}
} else {
return btn;
}
}
return null;
}
private void trySendEnterToDialog() {
// System.err.println("SendEnterToDialog");
EventObject ev = EventQueue.getCurrentEvent();
if (ev instanceof KeyEvent && (((KeyEvent) ev).getKeyCode() == KeyEvent.VK_ENTER)) {
if (ev.getSource() instanceof JComboBox && ((JComboBox) ev.getSource()).isPopupVisible()) {
return;
}
if (
ev.getSource() instanceof JTextComponent &&
((JTextComponent) ev.getSource()).getParent() instanceof JComboBox &&
((JComboBox) ((JTextComponent) ev.getSource()).getParent()).isPopupVisible()
) {
return;
}
JRootPane jrp = getRootPane();
if (jrp != null) {
JButton b = jrp.getDefaultButton();
if ((b != null) && b.isEnabled()) {
b.doClick();
}
}
}
}
private void trySendEnterToDialog(BaseTable bt) {
// System.err.println("SendEnterToDialog");
EventObject ev = EventQueue.getCurrentEvent();
if (ev instanceof KeyEvent && (((KeyEvent) ev).getKeyCode() == KeyEvent.VK_ENTER)) {
if (ev.getSource() instanceof JComboBox && ((JComboBox) ev.getSource()).isPopupVisible()) {
return;
}
if (
ev.getSource() instanceof JTextComponent &&
((JTextComponent) ev.getSource()).getParent() instanceof JComboBox &&
((JComboBox) ((JTextComponent) ev.getSource()).getParent()).isPopupVisible()
) {
return;
}
JRootPane jrp = bt.getRootPane();
if (jrp != null) {
JButton b = jrp.getDefaultButton();
if ((b != null) && b.isEnabled()) {
b.doClick();
}
}
}
}
private void workaround11364() {
JRootPane root = getRootPane();
if (root != null) {
JButton defaultButton = root.getDefaultButton();
if (defaultButton != null) {
defaultButton.doClick();
}
}
}
public void actionPerformed(ActionEvent e) {
int next = getSelectedRow() + (direction ? 1 : (-1));
//if we're off the end, try to find a sibling component to pass
//focus to
if ((next >= getRowCount()) || (next < 0)) {
if (!(BaseTable.this.getTopLevelAncestor() instanceof Dialog)) {
//If we're not in a dialog, we're in the main window - don't
//send focus somewhere because the winsys won't change the
//active mode
next = (next >= getRowCount()) ? 0 : (getRowCount() - 1);
} else if ((next >= getRowCount()) || (next < 0)) {
//if we're off the end, try to find a sibling component to pass
//focus to
//This code is a bit ugly, but works
Container ancestor = getFocusCycleRootAncestor();
//Find the next component in our parent's focus cycle
Component sibling = direction
? ancestor.getFocusTraversalPolicy().getComponentAfter(ancestor, BaseTable.this.getParent())
: ancestor.getFocusTraversalPolicy().getComponentBefore(ancestor, BaseTable.this);
//Often LayoutFocusTranferPolicy will return ourselves if we're
//the last. First try to find a parent focus cycle root that
//will be a little more polite
if (sibling == BaseTable.this) {
Container grandcestor = ancestor.getFocusCycleRootAncestor();
if (grandcestor != null) {
sibling = direction
? grandcestor.getFocusTraversalPolicy().getComponentAfter(grandcestor, ancestor)
: grandcestor.getFocusTraversalPolicy().getComponentBefore(grandcestor, ancestor);
ancestor = grandcestor;
}
}
//Okay, we still ended up with ourselves, or there is only one focus
//cycle root ancestor. Try to find the first component according to
//the policy
if (sibling == BaseTable.this) {
if (ancestor.getFocusTraversalPolicy().getFirstComponent(ancestor) != null) {
sibling = ancestor.getFocusTraversalPolicy().getFirstComponent(ancestor);
}
}
//If we're *still* getting ourselves, find the default button and punt
if (sibling == BaseTable.this) {
JRootPane rp = getRootPane();
JButton jb = rp.getDefaultButton();
if (jb != null) {
sibling = jb;
}
}
//See if it's us, or something we know about, and if so, just
//loop around to the top or bottom row - there's noplace
//interesting for focus to go to
if (sibling != null) {
if (sibling == BaseTable.this) {
//set the selection if there's nothing else to do
changeSelection(
direction ? 0 : (getRowCount() - 1), direction ? 0 : (getColumnCount() - 1), false,
false
);
} else {
//Request focus on the sibling
sibling.requestFocus();
}
return;
}
}
changeSelection(next, getSelectedColumn(), false, false);
}
if( getSelectionModel().getAnchorSelectionIndex() < 0 )
getSelectionModel().setAnchorSelectionIndex(next);
getSelectionModel().setLeadSelectionIndex(next);
}
@Override
public void actionPerformed(ActionEvent e) {
if (isEditing()) {
removeEditor();
}
int targetRow;
int targetColumn;
if (direction) {
if (getSelectedColumn() == getColumnCount()-1) {
targetColumn=0;
targetRow = getSelectedRow()+1;
} else {
targetColumn = getSelectedColumn()+1;
targetRow = getSelectedRow();
}
} else {
if (getSelectedColumn() == 0) {
targetColumn = getColumnCount()-1;
targetRow = getSelectedRow()-1;
} else {
targetRow = getSelectedRow();
targetColumn = getSelectedColumn() -1;
}
}
//if we're off the end, try to find a sibling component to pass
//focus to
if (targetRow >= getRowCount() || targetRow < 0) {
//This code is a bit ugly, but works
Container ancestor = getFocusCycleRootAncestor();
//Find the next component in our parent's focus cycle
Component sibling = direction ?
ancestor.getFocusTraversalPolicy().getComponentAfter(ancestor,
ETable.this) :
ancestor.getFocusTraversalPolicy().getComponentBefore(ancestor,
ETable.this);
//Often LayoutFocusTranferPolicy will return ourselves if we're
//the last. First try to find a parent focus cycle root that
//will be a little more polite
if (sibling == ETable.this) {
Container grandcestor = ancestor.getFocusCycleRootAncestor();
if (grandcestor != null) {
sibling = direction ? grandcestor.getFocusTraversalPolicy().getComponentAfter(grandcestor, ancestor) :
grandcestor.getFocusTraversalPolicy().getComponentBefore(grandcestor, ancestor);
ancestor = grandcestor;
}
}
//Okay, we still ended up with ourselves, or there is only one focus
//cycle root ancestor. Try to find the first component according to
//the policy
if (sibling == ETable.this) {
if (ancestor.getFocusTraversalPolicy().getFirstComponent(ancestor) != null) {
sibling = ancestor.getFocusTraversalPolicy().getFirstComponent(ancestor);
}
}
//If we're *still* getting ourselves, find the default button and punt
if (sibling == ETable.this) {
JRootPane rp = getRootPane();
JButton jb = rp.getDefaultButton();
if (jb != null) {
sibling = jb;
}
}
//See if it's us, or something we know about, and if so, just
//loop around to the top or bottom row - there's noplace
//interesting for focus to go to
if (sibling != null) {
if (sibling == ETable.this) {
//set the selection if there's nothing else to do
changeSelection(direction ? 0 : getRowCount()-1,
direction ? 0 : getColumnCount()-1,false,false);
} else {
//Request focus on the sibling
sibling.requestFocus();
}
return;
}
}
changeSelection (targetRow, targetColumn, false, false);
}