Java Reference
In-Depth Information
The screen sizes of PDAs differ significantly between different models. Figure 4.10 illustrates a set of
different PDAs from various vendors showing diverse screen formats and orientations. The Nokia 9210
has a horizontal display with a resolution of 640x240 pixels. Regular Palms have a screen resolution of
160x160 pixels. The HandEra 330 has a vertical screen format with a resolution of 240x320 pixels
similar to the screen of most PocketPCs such as the Compaq IPaq.
Figure 4.10. PDAs with different screen sizes available on the market.
In some cases, to make optimal use of the screen space available, a layout that dynamically adopts to
the screen resolution available might make the most sense. In order to demonstrate this concept,
assume that we would like to display two components with a fixed size. Depending on the screen
format, there might be space enough for both components, or only one component might fit on the
screen. If both components fit, they might fit only if horizontally or vertically arranged. It might also be
possible that not even a single component will fit in the available space. Clearly, we want to make
optimal use of the screen space available, so if both components fit, both should be displayed. If one
component fits, we need a control to switch the displayed component, similar to a JTabbedPane in
Swing. Finally, if not even one component fits, a scrollbar should allow the user to select a region of
the component(s) to be displayed.
How can this goal be achieved? It isn't as difficult as it might seem. Fortunately, there is a special
method doLayout() that the AWT system calls whenever an arrangement of a component is
required. So it is possible to overwrite this method, look at the space available, and then arrange the
child components accordingly.
For the example code, assume that width and height are the actual dimensions of the components
to be displayed. The following subclass of Panel arranges the two components rect1 and rect2
with respect to the width and height variables as described in the previous paragraph.
We start the implementation of doLayout() by removing all contained components, disabling a
choice control for switching between the images, and storing the actual dimensions in a local variable d :
class DynPanel extends Panel {
public void doLayout() {
removeAll();
cardChoice.setEnabled (false);
Dimension d = getSize();
 
Search WWH ::




Custom Search