S
softwarepearls_com
Hi,
Does anyone know how to force the focus on a Frame (or JFrame) which
has already been realized and is currently showing? Ideally the
solution should be platform neutral, but I'll be happy with anything
which works on either mac or windows.
BTW, I searched the forum's discussions on this topic, but all the
articles I found were from pre 1.4 days (the focus system was
rearchitected for 1.4).
For the record, I've tried the following (which doesn't work on Mac):
private void ensureWindowHasFocus() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
// if not visible or not active, make it visible
(which makes it active)
if (!topLevelWindow.isVisible() || !
topLevelWindow.isActive()) {
topLevelWindow.setVisible(true);
}
topLevelWindow.toFront();
topLevelWindow.requestFocus();
boolean success =
topLevelWindow.requestFocusInWindow();
System.out.println("Req focus in Window: " +
success);
}
});
} catch (InterruptedException exception) {
exception.printStackTrace();
} catch (InvocationTargetException exception) {
exception.printStackTrace();
}
}
Does anyone know how to force the focus on a Frame (or JFrame) which
has already been realized and is currently showing? Ideally the
solution should be platform neutral, but I'll be happy with anything
which works on either mac or windows.
BTW, I searched the forum's discussions on this topic, but all the
articles I found were from pre 1.4 days (the focus system was
rearchitected for 1.4).
For the record, I've tried the following (which doesn't work on Mac):
private void ensureWindowHasFocus() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
// if not visible or not active, make it visible
(which makes it active)
if (!topLevelWindow.isVisible() || !
topLevelWindow.isActive()) {
topLevelWindow.setVisible(true);
}
topLevelWindow.toFront();
topLevelWindow.requestFocus();
boolean success =
topLevelWindow.requestFocusInWindow();
System.out.println("Req focus in Window: " +
success);
}
});
} catch (InterruptedException exception) {
exception.printStackTrace();
} catch (InvocationTargetException exception) {
exception.printStackTrace();
}
}