(( CHECK THINK WERE CONFUSING DEVIC EAND CONFIGURATION ==

Coping with different screen sizes

This happens to all developers at one time or another. You painstakingly design a beautiful user interface that looks perfect on your Deluxtechnik 21'' LCD monitor in 1800x1200 resolution. But when your client loads it on their lowly Scheisstechnik CRT at 800x600 resolution, the intricate multi-panel interface that you so lovingly laboured over suddenly looks an incomprehensible mess. So what can we do?

There are two keys to coping with different screen sizes:

Tools and tips for working with different screen sizes

Java provides a number of useful methods for tailoring your application to different screen sizes. The most obvious is Toolkit.getScreenSize(). This returns the dimensions of the client's screen (or their "main" screen if they have several active), in pixels:

Dimension sz = Toolkit.getDefaultToolkit().getScreenSize();
System.out.println("Screen size: " + sz.width + " x " + sz.height);

In principle, we can then calculate a suitable size for a window from the screen size. There are just a couple of complications:

Now, I have a confession to make: most of the time, I'm not that fussy and I'm quite lazy. I sometimes decide on the size of a window by calling Toolkit.getScreenSize() and "subtracting a bit" from each dimension. In practice, hardly anybody in the universe has mutliple displays connected to their computer, and different users have different preferences as to what proportion of the screen they'd like a window to take up. So I'm assuming if I open something that's "vaguely sensible", they'll then adjust it slightly to their tastes. (I also do a fair amount of web-based development, where it's easy to roll out a new version if I suddenly discover a spate of multi-monitor users.)

However, it is possible— and theoretically more correct— to (a) verify which display a given window is opened on (or about to open on), and (b) query the size of that particular display. In Java land, each display is a GraphicsConfiguration. You can query a JFrame (or actually any component) to find out which GraphicsConfiguration it is associated with. Then, we call the GC's getBounds method to find out the screen bounds in pixels:

JFrame f = new JFrame();
GraphicsConfiguration gc = f.getGraphicsConfiguration();
Rectangle bounds = gc.getBounds();
int screenWidth = bounds.width;
int screenHeight = bounds.height;

You can also find out the screen insets of that display. The screen insets are the space taken up by things such as toolbars and menu bars. Confusingly, you go via the Toolkit class rather than querying the GC directly:

Insets ins = Toolkit.getDefaultToolkit().getScreenInsets(gc);

In case you've not used one before, an Insets object has left, right, top and bottom properties.

Font sizes and screen resolution

As well as the screen size, it is also useful to get an estimate of the screen resolution: that is, approximately how many pixels there are per (say) inch. The usual measurement of resolution is dots per inch (DPI), which you've probably heard of at least in relation to scanners and desktop publishing. The Toolkit class provides another static method for querying the approximate DPI of the


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.