5.11 The Active Shell, All Shells, and Focus Control
Displays keep a list of all shells that were created, the active shell, and the focus control. This section discusses the methods in class Display that provide access to this state. Because the methods to set the active shell and focus control are implemented in the classes Shell and Control, they are discussed in the appropriate sections of the Controls, Composites, Groups, and Shells chapter.
5.11.1 Getting the Active Shell
At any given time, one shell at most is active on the desktop. Window systems often indicate the active shell by drawing the title bar in a different color or style. If the active shell was created on a particular display, it can be queried using the getActiveShell() method.
getActiveShell()
Returns the active shell if it was created on this display. If there is no active shell or the active shell belongs to another application, getActiveShell() returns null.
On Windows, when the user clicks on the background of the desktop, no shell is active. In this case, getActiveShell() will return null.
The following code fragment finds and disposes of the active shell.
Shell shell = display.getActiveShell();
if (shell != null) shell.dispose();
5.11.2 Getting the List of Shells
The list of all shells for the display can be queried using the getShells() method.
getShells()
Returns every shell that was created on the display, including shells that are children of other shells.
The following code fragment queries the list of shells and minimizes all but the active one.
Shell shell = display.getActiveShell();
if (shell != null) {
while (shell.getParent() != null) {
shell = shell.getParent().getShell();
}
}
if (shell != null) {
Shell[] shells = display.getShells();
for (int i = 0; i < shells.length; i++) {
if (shells[i].getParent() == null) {
if (shells[i] != shell) {
shells[i].setMinimized(true);
}
}
}
}
Notice that the code minimizes only the shells that have no parent. This works because minimizing a shell will cause all shells that are children of that shell to be hidden.
5.11.3 Getting the Focus Control
At most, one widget on the desktop can have keyboard focus (see the chapter The Keyboard). You can query the control that has keyboard focus using the following Display method.
getFocusControl()
Returns the control that will receive keyboard events. If there is no widget that has keyboard focus or if the control that has keyboard focus belongs to some other application, null is returned. To set the focus control, use the method setFocus() in the class Control.
The following code fragment prints the focus control.
Control control = display.getFocusControl();
System.out.println("Focus control is " + control);
|