Windows Java tip: How to control window decorations

Technologies: Java 5+, Windows XP/Vista/7

Windows automatically adds a window frame to all Java windows, but there is more that you can do to make sure Java windows look and feel right on Windows XP, Vista, 7, and beyond. This article discusses Swing settings to control the appearance of window decorations on a PC.

Introduction

Every Java application window on a Windows PC gets a window frame, title bar, title text, window icon, buttons, and a pull-down menu to close, minimize, and maximize the window. These make Java windows indistinguishable from standard PC windows. (See Windows chapter of Microsoft's Windows User Experience Interaction Guidelines.)

Anatomy of a Windows window
Figure 1. The anatomy of a Java window on a Windows PC.

Handling windows themes

Themeing lets users personalize their PC with their own choice of color scheme, fonts, and window decorations. From a user's perspective, it's kind of fun. Here's a few (yes, even the Mac-style window below is a Windows theme). See the Appendix for a lot more samples.

A sampling of Windows themes
Figure 2. A sampling of Windows themes.

From an IT support perspective, themeing is a headache. If every PC can look different, IT support can't reliably say "Click the red "×" button in the upper right corner to close the window." The user's theme might put that button in the upper left corner instead, and it might not be red, or contain an "×".

From a Java development perspective, themeing is again a headache. The user can change the default fonts and color schemes used by applications, including Java. Different fonts will change the size of buttons and labels, and shift your layout. Different color schemes may clash with your own chosen colors.

To protect your application from theme changes, you can either set fonts and colors explicitly, or code defensively. Philosophically, it's a bad idea to override the user's choice — it's their PC, not yours. Also, if the user has chosen a high-contrast color scheme because they are visually impaired, overriding their choices can make your application unusable.

Instead of overriding the user, adapt to their choices. Use layout managers so that your layouts will adjust to different fonts. Use the theme's colors available to you in the SystemColor class. If you have to choose new colors, use an algorithm that computes colors based upon the user's theme colors. If they've chosen light text on a dark background, your algorithm should do the same.

Controlling the title bar buttons

AWT Frame and Swing JFrame windows (with standard themes) always have all three title bar buttons (minimize, maximize, and close). All three are enabled by default. Calling setResizable( false ) prevents the frame from being resized by the user, disables the middle maximize button, and disables resizing choices on the window's pull-down menu. Vista Basic, but not Vista Aero, also reduces the thick window border for non-resizable windows.

JFrame frame = new JFrame( "Not resizable" );
frame.setResizable( false );
Frames on a Windows PC
Figure 3. Resizable and non-resizable frames on a Windows PC.

AWT Dialog and Swing JDialog windows always have just one button (close). Dialogs cannot be minimized to icons, but they can be maximized to full screen using a pull-down menu choice. Like a frame, calling setResizable( false ) on a dialog prevents it from being resized by the user and disables resizing choices on the pull-down menu. Vista Basic, but not Vista Aero, also reduces the window border for non-resizable windows.

JDialog dialog = new JDialog( parent, "Not resizable" );
dialog.setResizable( false );
Dialogs on a Windows PC
Figure 4. Resizable and non-resizable dialogs on a Windows PC.

For frames, the window icon can be set from Java (see below). Dialogs use the window icon of their parent frame. However, since the window icon is hidden on non-resizable dialogs, you cannot count on the icon to help users know which frame is the parent of which dialog.

Normally, users can get the window menu by left-clicking on the window icon. For non-resizable dialogs that remove the windows icon, users can still get the window menu by right-clicking on the title bar. However, beginning users might not know this.

Setting the title bar text

Frame and dialog title text is shown left-adjusted on the title bar (for standard themes). The font and color cannot be changed from Java, but the title text can be set on the constructor, or by calling setTitle( ).

JFrame frame = new JFrame( );
frame.setTitle( "Text" );

Windows includes support for UTF-8 Unicode characters in window titles. You can use this to show proper characters for the world's languages, or to show equations, dingbats, and other special characters.

Window titles with Unicode characters on a Windows PC
Figure 5. Unicode characters in Vista window titles on a Windows PC.

The proper display of Unicode characters depends upon the version of Windows. Unicode has over 100,000 characters and no version of Windows (or any OS) supports all of them yet. Vista and 7 fonts have better support than XP, and XP better than 95 or 98. Latin language letters and accents, Greek letters, and basic math symbols are well supported. Bars, blocks, bullets, dingbats, and glyphs from other languages are less well supported.

Here is the same window title text shown above, but now on Windows XP. The small boxes are drawn whenever XP does not recognize the character — which is most of the time. If your target market includes Windows XP users, using Unicode characters in window titles (or anywhere in the window) may not work well.

Window titles with Unicode characters on a Windows PC
Figure 6. Unicode characters in XP window titles on a Windows PC.

Notice also that the amount of text visible in a window title bar depends on the theme. For Windows XP, the buttons are smaller and titles can be longer.

Setting the title bar window icon

Windows adds an icon to the title bar for frames and resizable dialogs (for most themes). Users can click on the icon to show a menu of window operations. The same menu is available by right-clicking anywhere on the title bar.

To set the window icon, call setIconImage( ) on a Frame or JFrame. Child dialogs of the frame use the same icon. The default icon is the Java steaming coffee cup. Setting the image to a null reverts to this default — there is no way to remove the image entirely (except by using a transparent image).

JFrame frame = new JFrame( "Title" );
frame.setIconImage( image );
Window icons on a Windows PC
Figure 7. Frames with default and custom window icons on a Windows PC.

The same icon is used when a frame is iconified to the task bar, shown in the task switcher (2D version), or listed in the Task Manager. The title bar and task bar each use a 16 x 16 icon, while the task switcher uses a 32 x 32 icon. Java/Windows automatically scales icons to these sizes. Since you can only provide a single icon at one resolution for a frame, for the best results use a 32 x 32 image, or larger, but test that it shrinks well to 16 x 16.

Unfocusing the title bar

The front window on a Windows PC has the focus and is drawn with a colored title bar and buttons (for standard themes). Back windows do not have the focus and are drawn with a dimmed title bar and uncolored buttons.

Calling setFocusableWindowState( false ) marks a window as a floating palette that cannot accept the focus (though its non-text field components can). Windows draws unfocusable windows the same as background windows, giving them a dimmed title bar and buttons. This is normally only done on dialogs.

If you make an unfocusable dialog modal, you'll confuse users. Such a window is dimmed like a background window, but it actually owns the focus and demands a user response.

JDialog dialog = new JDialog( "Not focusable" );
dialog.setResizable( false );
dialog.setFocusableWindowState( false );
Title bar focus on windows on a Windows PC
Figure 8. Focusable and unfocusable dialogs on a Windows PC.

Removing the title bar

Calling setUndecorated( true ) on a dialog or frame, or creating an instance of a JWindow or Window, creates a window without a title bar or buttons. This is useful for creating temporary splash windows.

JDialog dialog = new JDialog( parent );
dialog.setUndecorated( true );
Undecorated window on a Windows PC
Figure 9. Undecorated window on a Windows PC.

Since moving a window is done by a click-and-drag on the title bar, and resizing is done by click-and-drag on the window frame, if you remove a window's decorations the window can't be moved or resized normally. Also, without the buttons on the title bar, the window can't be closed, minimized, or maximized. You will need to implement these features yourself if you intend them to be available.

Using look-and-feel-specific window decorations

Some Java look and feels can provide their own title bar, icons, and window border. If a look and feel supports this feature, its getSupportsWindowDecorations( ) method will return true.

LookAndFeel laf = UIManager.getLookandFeel( );
if ( laf.getSupportsWindowDecorations( ) )
{
    // Look and feel window decorations are supported
}

Most look and feels do not support this feature. At present, the only major look and feel that does is Sun's "Metal". The "Windows" and "Windows Classic" look and feels on Windows do not.

When this feature is supported, frames and dialogs may be marked undecorated and the look and feel asked to add a specific decoration style. The available styles include: NONE, FRAME, PLAIN_DIALOG, INFORMATION_DIALOG, QUESTION_DIALOG, WARNING_DIALOG, ERROR_DIALOG, COLOR_CHOOSER, and FILE_CHOOSER.

JDialog dialog = new JDialog( parent, "Plain" );
dialog.setUndecorated( true );
dialog.setDefaultLookAndFeelDecorated( false );
dialog.getRootPane( ).setWindowDecorationStyle( JDialog.PLAIN_DIALOG );

The NONE style creates an undecorated window. The others add custom window decorations. Here's what they look like for "Metal" on a Windows PC:

Window decorations by Metal on a Windows PC
Figure 10. Window decorations from the "Metal" look and feel on a Windows PC.

This feature should be used with care, if ever. These windows don't look Windows-like and they may confuse users that expect PC windows on a PC. One possible use is in a kiosk application where the PC's OS is hidden from view and only windows like these are shown.

If you try to use any of these with a look and feel that doesn't support them (such as "Windows"), all of the windows will be undecorated.

Conclusions & Recommendations

The Java Swing calls shown above are standardized for setting window icons, resizability, focusability, and so forth. But different platforms can still behave differently. Some Windows vs. Mac differences of note:

  • On Windows, frames and dialogs have window icons. On Macs, they don't. Calling setIconImage(icon) on a Mac does nothing.
  • On Windows, iconified frames show their window title and/or window icon on the Windows task bar. On Macs, there is no task bar. On the Mac dock, the left side shows application (not window) icons, while the right side shows window icons. But these window icons are thumbnails of the window, not the window icon set in Java. And the dock's application icon can't be set using standard Java Swing calls. You have to use Apple-specific Java classes.
  • On Windows, resizable frames and dialogs have borders that users click-and-drag to resize the window. On Macs, there are no window borders on the left, right, and bottom. Instead, the Mac adds a resize icon to the lower right of a window (you have to leave room for it) and users click-and-drag it to resize the window.
  • On Windows, resizable dialogs don't have a maximize button though they still can be maximized with a window menu choice. On Macs, resizable dialogs have a maximize button, but no window menu.
  • On Windows, a window may or may not have a drop shadow, depending upon the theme. On Macs, there is always a drop shadow, but Java application's can turn it off. If you are making a splash screen, the Windows drop shadow will look odd.
  • On Windows, the title bar size is entirely up to the theme. On Macs, a Java application can request a small-sized title bar used for tool palettes.
  • On Windows, Unicode support varies considerably from XP to Vista and 7. On Macs, Unicode has been well-supported for long enough that the installed base of Mac OS X all has it.
  • On Windows, the application's menu bar is at the top of the window. On Macs, the menu bar is at the top of the screen instead. The Mac menu bar always shows the application's name, so by convention window title bars only show a document name. But on Windows, the OS doesn't show the application's name anywhere automatically. Instead, applications conventionally add their name to the title bar themselves.
  • On Windows, it is a common convention to add a "*" to a document's title bar to mark the document as modified and not yet saved. On the Mac, there are custom window properties to add a standardized title bar mark instead of the "*".
  • On Windows, the title bar only contains the title text and window buttons. On the Mac, a window can have a document icon too (not the same as a window icon). The OS automatically finds the right icon for different types of documents.

Please see my article Mac Java tip: How to control window decorations for a discussion of Mac window decoration control from Java.

Linux is another variable. Window managers and themes vary considerably from one branded version of Linux to the next. Also, the high technical level of most Linux users makes it more likely that they've changed their theme to suit their own tastes. And that makes it even more important to use layout managers and avoid assumptions about color schemes and fonts in your Java application.

Appendix: Windows themes

Here's a look at some of the themes in the current and recent generations of Windows.

Windows 95 and 98 themes

Windows 95 and 98 standard themes used blue title bars and gray buttons. Users could change the title bar colors, background colors, fonts, border thickness, and more. While Windows 95 and 98 have been replaced by newer versions of Windows, the appearance of these windows lives on as selectable themes in XP, Vista, and 7.

Windows 95, 98 themes
Figure 11. Windows 95 & 98-like themes
available in Windows XP, Vista, and 7.

Windows XP themes

Windows XP used three new themes with rounded blue, silver, or olive green title bars. Users could use these or revert to the look of Windows 95 and 98.

Windows XP themes
Figure 12. Windows XP themes.

Windows Vista and 7 themes

Windows Vista and 7 add two new themes: Vista Basic and Vista Aero. Both provide flat glossy looks. Vista Aero adds translucent window borders and drop shadows on faster graphics hardware for Vista editions other than Home Basic. Users can alter the window frame colors or revert to Windows 95 and 98 themes (but not XP themes).

Windows Vista and 7 themes
Figure 13. Windows Vista and 7 themes.

Windows Vista and 7 also include several high-contrast themes for the visually impaired.

Windows Vista and 7 themes
Figure 14. High-contrast themes for Windows Vista and 7.

Custom themes

Third-party products for all versions of Windows can radically alter window decorations, fonts, and color schemes (notably WindowBlinds from StarDock). Here are a few examples. Note that three of them omit the window icon, two move the buttons to the left side, three change the title bar font, and all of them change the window background color. There are hundreds more themes available that users can choose to fit their personal style.

Custom windows themes
Figure 15. Custom themes installed with third-party utilities.

Further reading

Related articles at NadeauSoftware.com

Web articles and specifications

Comments

Most Useful for novice

Thank you for providing relative information about "How to control window decorations".

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

Nadeau software consulting
Nadeau software consulting