Application.DoEvents() -> Equivalent function in java?
I know that using threads is more efficient than using c# DoEv开发者_如何学编程ents(), but I was still wondering whether there is an equivalent function in java. I googled for it, but I couldn't find anything.
You can use Thread.yield()
, which is the java counterpart to relinquish the control of the processors voluntarily.
You can use EventQueue.invokeLater()
to append a Runnable
after all pending events. This have a result similar to C#'s DoEvents()
that comes before the code you put inside the Runnable.run()
method.
See Java documentation for EventQueue.
For example, if you want to let all GUI controls to lose the focus and their lost focus events to be execute, you can use the following code:
@Override
public void windowClosing(WindowEvent e){
// Clear the focus to allow last changes to be noted.
KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
// We want to let other events (e.g. lost focus) run before we start closing.
EventQueue.invokeLater( new Runnable() {
@Override public void run() {
// Do actual closing...
}
});
}
精彩评论