下面列出了java.awt.KeyboardFocusManager#getFocusOwner ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void doReturn() {
KeyboardFocusManager fmgr =
KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component focusOwner = fmgr.getFocusOwner();
if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
return;
}
MenuSelectionManager msm = MenuSelectionManager.defaultManager();
MenuElement path[] = msm.getSelectedPath();
MenuElement lastElement;
if(path.length > 0) {
lastElement = path[path.length-1];
if(lastElement instanceof JMenu) {
MenuElement newPath[] = new MenuElement[path.length+1];
System.arraycopy(path,0,newPath,0,path.length);
newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
msm.setSelectedPath(newPath);
} else if(lastElement instanceof JMenuItem) {
JMenuItem mi = (JMenuItem)lastElement;
if (mi.getUI() instanceof BasicMenuItemUI) {
((BasicMenuItemUI)mi.getUI()).doClick(msm);
}
else {
msm.clearSelectedPath();
mi.doClick(0);
}
}
}
}
public static void main(String[] args) throws InterruptedException, java.lang.reflect.InvocationTargetException
{
final Frame frame = new Frame("the test");
frame.setLayout(new FlowLayout());
final Button btn1 = new Button("button 1");
frame.add(btn1);
frame.add(new Button("button 2"));
frame.add(new Button("button 3"));
frame.pack();
frame.setVisible(true);
Robot r = Util.createRobot();
Util.waitForIdle(r);
Util.clickOnComp(btn1, r);
Util.waitForIdle(r);
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
if (kfm.getFocusOwner() != btn1) {
throw new RuntimeException("test error: can not set focus on " + btn1 + ".");
}
EventQueue.invokeAndWait(new Runnable() {
public void run() {
final int n_comps = frame.getComponentCount();
for (int i = 0; i < n_comps; ++i) {
frame.getComponent(i).setVisible(false);
}
}
});
Util.waitForIdle(r);
final Component focus_owner = kfm.getFocusOwner();
if (focus_owner != null && !focus_owner.isVisible()) {
throw new RuntimeException("we have invisible focus owner");
}
System.out.println("test passed");
frame.dispose();
}
private void doReturn() {
KeyboardFocusManager fmgr =
KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component focusOwner = fmgr.getFocusOwner();
if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
return;
}
MenuSelectionManager msm = MenuSelectionManager.defaultManager();
MenuElement path[] = msm.getSelectedPath();
MenuElement lastElement;
if(path.length > 0) {
lastElement = path[path.length-1];
if(lastElement instanceof JMenu) {
MenuElement newPath[] = new MenuElement[path.length+1];
System.arraycopy(path,0,newPath,0,path.length);
newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
msm.setSelectedPath(newPath);
} else if(lastElement instanceof JMenuItem) {
JMenuItem mi = (JMenuItem)lastElement;
if (mi.getUI() instanceof BasicMenuItemUI) {
((BasicMenuItemUI)mi.getUI()).doClick(msm);
}
else {
msm.clearSelectedPath();
mi.doClick(0);
}
}
}
}
public void requestFocus() {
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component focusOwner = kfm.getFocusOwner();
if (focusOwner != null && SwingUtilities.isDescendingFrom(focusOwner, getComponent())) {
return;
}
getComponent().requestFocus();
}
/**
* Returns the context object which corresponds to the
* area of focus within this provider's component. Null
* is returned when there is no context.
* @param event popup event which corresponds to this request.
* May be null for key-stroke or other non-mouse event.
*/
@Override
public ActionContext getActionContext(MouseEvent event) {
Component c = getComponent();
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component focusedComponent = kfm.getFocusOwner();
if (focusedComponent != null && SwingUtilities.isDescendingFrom(focusedComponent, c)) {
c = focusedComponent;
}
return createContext(c, null);
}
private void doReturn() {
KeyboardFocusManager fmgr =
KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component focusOwner = fmgr.getFocusOwner();
if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
return;
}
MenuSelectionManager msm = MenuSelectionManager.defaultManager();
MenuElement path[] = msm.getSelectedPath();
MenuElement lastElement;
if(path.length > 0) {
lastElement = path[path.length-1];
if(lastElement instanceof JMenu) {
MenuElement newPath[] = new MenuElement[path.length+1];
System.arraycopy(path,0,newPath,0,path.length);
newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
msm.setSelectedPath(newPath);
} else if(lastElement instanceof JMenuItem) {
JMenuItem mi = (JMenuItem)lastElement;
if (mi.getUI() instanceof BasicMenuItemUI) {
((BasicMenuItemUI)mi.getUI()).doClick(msm);
}
else {
msm.clearSelectedPath();
mi.doClick(0);
}
}
}
}
public static void main(String[] args) throws InterruptedException, java.lang.reflect.InvocationTargetException
{
final Frame frame = new Frame("the test");
frame.setLayout(new FlowLayout());
final Button btn1 = new Button("button 1");
frame.add(btn1);
frame.add(new Button("button 2"));
frame.add(new Button("button 3"));
frame.pack();
frame.setVisible(true);
Robot r = Util.createRobot();
Util.waitForIdle(r);
Util.clickOnComp(btn1, r);
Util.waitForIdle(r);
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
if (kfm.getFocusOwner() != btn1) {
throw new RuntimeException("test error: can not set focus on " + btn1 + ".");
}
EventQueue.invokeAndWait(new Runnable() {
public void run() {
final int n_comps = frame.getComponentCount();
for (int i = 0; i < n_comps; ++i) {
frame.getComponent(i).setVisible(false);
}
}
});
Util.waitForIdle(r);
final Component focus_owner = kfm.getFocusOwner();
if (focus_owner != null && !focus_owner.isVisible()) {
throw new RuntimeException("we have invisible focus owner");
}
System.out.println("test passed");
frame.dispose();
}
private void doReturn() {
KeyboardFocusManager fmgr =
KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component focusOwner = fmgr.getFocusOwner();
if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
return;
}
MenuSelectionManager msm = MenuSelectionManager.defaultManager();
MenuElement path[] = msm.getSelectedPath();
MenuElement lastElement;
if(path.length > 0) {
lastElement = path[path.length-1];
if(lastElement instanceof JMenu) {
MenuElement newPath[] = new MenuElement[path.length+1];
System.arraycopy(path,0,newPath,0,path.length);
newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
msm.setSelectedPath(newPath);
} else if(lastElement instanceof JMenuItem) {
JMenuItem mi = (JMenuItem)lastElement;
if (mi.getUI() instanceof BasicMenuItemUI) {
((BasicMenuItemUI)mi.getUI()).doClick(msm);
}
else {
msm.clearSelectedPath();
mi.doClick(0);
}
}
}
}
public static void main(String[] args) throws InterruptedException, java.lang.reflect.InvocationTargetException
{
final Frame frame = new Frame("the test");
frame.setLayout(new FlowLayout());
final Button btn1 = new Button("button 1");
frame.add(btn1);
frame.add(new Button("button 2"));
frame.add(new Button("button 3"));
frame.pack();
frame.setVisible(true);
Robot r = Util.createRobot();
Util.waitForIdle(r);
Util.clickOnComp(btn1, r);
Util.waitForIdle(r);
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
if (kfm.getFocusOwner() != btn1) {
throw new RuntimeException("test error: can not set focus on " + btn1 + ".");
}
EventQueue.invokeAndWait(new Runnable() {
public void run() {
final int n_comps = frame.getComponentCount();
for (int i = 0; i < n_comps; ++i) {
frame.getComponent(i).setVisible(false);
}
}
});
Util.waitForIdle(r);
final Component focus_owner = kfm.getFocusOwner();
if (focus_owner != null && !focus_owner.isVisible()) {
throw new RuntimeException("we have invisible focus owner");
}
System.out.println("test passed");
frame.dispose();
}
private void doReturn() {
KeyboardFocusManager fmgr =
KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component focusOwner = fmgr.getFocusOwner();
if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
return;
}
MenuSelectionManager msm = MenuSelectionManager.defaultManager();
MenuElement path[] = msm.getSelectedPath();
MenuElement lastElement;
if(path.length > 0) {
lastElement = path[path.length-1];
if(lastElement instanceof JMenu) {
MenuElement newPath[] = new MenuElement[path.length+1];
System.arraycopy(path,0,newPath,0,path.length);
newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
msm.setSelectedPath(newPath);
} else if(lastElement instanceof JMenuItem) {
JMenuItem mi = (JMenuItem)lastElement;
if (mi.getUI() instanceof BasicMenuItemUI) {
((BasicMenuItemUI)mi.getUI()).doClick(msm);
}
else {
msm.clearSelectedPath();
mi.doClick(0);
}
}
}
}
/**
* Method mouseMoved
*
* @param e
*/
public void mouseMoved(MouseEvent e) {
KeyboardFocusManager focuser =
KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component focusOwner = focuser.getFocusOwner();
if (focusOwner != null && !(focusOwner instanceof JTextComponent)) {
requestFocusInWindow();
}
}
private void doReturn() {
KeyboardFocusManager fmgr =
KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component focusOwner = fmgr.getFocusOwner();
if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
return;
}
MenuSelectionManager msm = MenuSelectionManager.defaultManager();
MenuElement path[] = msm.getSelectedPath();
MenuElement lastElement;
if(path.length > 0) {
lastElement = path[path.length-1];
if(lastElement instanceof JMenu) {
MenuElement newPath[] = new MenuElement[path.length+1];
System.arraycopy(path,0,newPath,0,path.length);
newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
msm.setSelectedPath(newPath);
} else if(lastElement instanceof JMenuItem) {
JMenuItem mi = (JMenuItem)lastElement;
if (mi.getUI() instanceof BasicMenuItemUI) {
((BasicMenuItemUI)mi.getUI()).doClick(msm);
}
else {
msm.clearSelectedPath();
mi.doClick(0);
}
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
lastFocusOwner = kfm.getFocusOwner();
if (lastFocusOwner != null) {
lastFocusOwner.addKeyListener(this);
}
if (!canDrag(e)) {
setSelectCursor();
}
}
private void doReturn() {
KeyboardFocusManager fmgr =
KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component focusOwner = fmgr.getFocusOwner();
if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
return;
}
MenuSelectionManager msm = MenuSelectionManager.defaultManager();
MenuElement path[] = msm.getSelectedPath();
MenuElement lastElement;
if(path.length > 0) {
lastElement = path[path.length-1];
if(lastElement instanceof JMenu) {
MenuElement newPath[] = new MenuElement[path.length+1];
System.arraycopy(path,0,newPath,0,path.length);
newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
msm.setSelectedPath(newPath);
} else if(lastElement instanceof JMenuItem) {
JMenuItem mi = (JMenuItem)lastElement;
if (mi.getUI() instanceof BasicMenuItemUI) {
((BasicMenuItemUI)mi.getUI()).doClick(msm);
}
else {
msm.clearSelectedPath();
mi.doClick(0);
}
}
}
}
public static void main(String[] args) throws InterruptedException, java.lang.reflect.InvocationTargetException
{
final Frame frame = new Frame("the test");
frame.setLayout(new FlowLayout());
final Button btn1 = new Button("button 1");
frame.add(btn1);
frame.add(new Button("button 2"));
frame.add(new Button("button 3"));
frame.pack();
frame.setVisible(true);
Robot r = Util.createRobot();
Util.waitForIdle(r);
Util.clickOnComp(btn1, r);
Util.waitForIdle(r);
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
if (kfm.getFocusOwner() != btn1) {
throw new RuntimeException("test error: can not set focus on " + btn1 + ".");
}
EventQueue.invokeAndWait(new Runnable() {
public void run() {
final int n_comps = frame.getComponentCount();
for (int i = 0; i < n_comps; ++i) {
frame.getComponent(i).setVisible(false);
}
}
});
Util.waitForIdle(r);
final Component focus_owner = kfm.getFocusOwner();
if (focus_owner != null && !focus_owner.isVisible()) {
throw new RuntimeException("we have invisible focus owner");
}
System.out.println("test passed");
frame.dispose();
}
private void doReturn() {
KeyboardFocusManager fmgr =
KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component focusOwner = fmgr.getFocusOwner();
if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
return;
}
MenuSelectionManager msm = MenuSelectionManager.defaultManager();
MenuElement[] path = msm.getSelectedPath();
MenuElement lastElement;
if(path.length > 0) {
lastElement = path[path.length-1];
if(lastElement instanceof JMenu) {
MenuElement[] newPath = new MenuElement[path.length+1];
System.arraycopy(path,0,newPath,0,path.length);
newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
msm.setSelectedPath(newPath);
} else if(lastElement instanceof JMenuItem) {
JMenuItem mi = (JMenuItem)lastElement;
if (mi.getUI() instanceof BasicMenuItemUI) {
((BasicMenuItemUI)mi.getUI()).doClick(msm);
}
else {
msm.clearSelectedPath();
mi.doClick(0);
}
}
}
}
public static void main(String[] args) throws InterruptedException, java.lang.reflect.InvocationTargetException
{
final Frame frame = new Frame("the test");
frame.setLayout(new FlowLayout());
final Button btn1 = new Button("button 1");
frame.add(btn1);
frame.add(new Button("button 2"));
frame.add(new Button("button 3"));
frame.pack();
frame.setVisible(true);
Robot r = Util.createRobot();
Util.waitForIdle(r);
Util.clickOnComp(btn1, r);
Util.waitForIdle(r);
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
if (kfm.getFocusOwner() != btn1) {
throw new RuntimeException("test error: can not set focus on " + btn1 + ".");
}
EventQueue.invokeAndWait(new Runnable() {
public void run() {
final int n_comps = frame.getComponentCount();
for (int i = 0; i < n_comps; ++i) {
frame.getComponent(i).setVisible(false);
}
}
});
Util.waitForIdle(r);
final Component focus_owner = kfm.getFocusOwner();
if (focus_owner != null && !focus_owner.isVisible()) {
throw new RuntimeException("we have invisible focus owner");
}
System.out.println("test passed");
frame.dispose();
}
public static void main(String[] args) throws InterruptedException, java.lang.reflect.InvocationTargetException
{
final Frame frame = new Frame("the test");
frame.setLayout(new FlowLayout());
final Button btn1 = new Button("button 1");
frame.add(btn1);
frame.add(new Button("button 2"));
frame.add(new Button("button 3"));
frame.pack();
frame.setVisible(true);
Robot r = Util.createRobot();
Util.waitForIdle(r);
Util.clickOnComp(btn1, r);
Util.waitForIdle(r);
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
if (kfm.getFocusOwner() != btn1) {
throw new RuntimeException("test error: can not set focus on " + btn1 + ".");
}
EventQueue.invokeAndWait(new Runnable() {
public void run() {
final int n_comps = frame.getComponentCount();
for (int i = 0; i < n_comps; ++i) {
frame.getComponent(i).setVisible(false);
}
}
});
Util.waitForIdle(r);
final Component focus_owner = kfm.getFocusOwner();
if (focus_owner != null && !focus_owner.isVisible()) {
throw new RuntimeException("we have invisible focus owner");
}
System.out.println("test passed");
frame.dispose();
}
private void doReturn() {
KeyboardFocusManager fmgr =
KeyboardFocusManager.getCurrentKeyboardFocusManager();
Component focusOwner = fmgr.getFocusOwner();
if(focusOwner != null && !(focusOwner instanceof JRootPane)) {
return;
}
MenuSelectionManager msm = MenuSelectionManager.defaultManager();
MenuElement path[] = msm.getSelectedPath();
MenuElement lastElement;
if(path.length > 0) {
lastElement = path[path.length-1];
if(lastElement instanceof JMenu) {
MenuElement newPath[] = new MenuElement[path.length+1];
System.arraycopy(path,0,newPath,0,path.length);
newPath[path.length] = ((JMenu)lastElement).getPopupMenu();
msm.setSelectedPath(newPath);
} else if(lastElement instanceof JMenuItem) {
JMenuItem mi = (JMenuItem)lastElement;
if (mi.getUI() instanceof BasicMenuItemUI) {
((BasicMenuItemUI)mi.getUI()).doClick(msm);
}
else {
msm.clearSelectedPath();
mi.doClick(0);
}
}
}
}
public static void main(String[] args) throws InterruptedException, java.lang.reflect.InvocationTargetException
{
final Frame frame = new Frame("the test");
frame.setLayout(new FlowLayout());
final Button btn1 = new Button("button 1");
frame.add(btn1);
frame.add(new Button("button 2"));
frame.add(new Button("button 3"));
frame.pack();
frame.setVisible(true);
Robot r = Util.createRobot();
Util.waitForIdle(r);
Util.clickOnComp(btn1, r);
Util.waitForIdle(r);
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
if (kfm.getFocusOwner() != btn1) {
throw new RuntimeException("test error: can not set focus on " + btn1 + ".");
}
EventQueue.invokeAndWait(new Runnable() {
public void run() {
final int n_comps = frame.getComponentCount();
for (int i = 0; i < n_comps; ++i) {
frame.getComponent(i).setVisible(false);
}
}
});
Util.waitForIdle(r);
final Component focus_owner = kfm.getFocusOwner();
if (focus_owner != null && !focus_owner.isVisible()) {
throw new RuntimeException("we have invisible focus owner");
}
System.out.println("test passed");
frame.dispose();
}