Java >
Guidelines Home Page >  Java Look and Feel Design Guidelines
Part II: Fundamental Java Application Design
6: Behavior
> Keyboard Operations
  Previous Next Contents/Index/Search

Keyboard Operations

The Java look and feel assumes a PC-style keyboard. The standard ASCII keys are used, along with the following modifier keys: Shift, Control, and Alt (Option on the Macintosh); the function keys F1 through F12; the four arrow keys; Delete, Backspace, Home, End, Page Up, and Page Down. Enter and Return are equivalent. (Return does not appear on PC keyboards.)

A modifier key is a key that does not produce an alphanumeric character but can be used in combination with other keys to alter the meaning of those keys. Typical modifier keys in Java look and feel applications are Shift, Control, and Alt.

This section provides recommendations for the use of keyboard operations, which include keyboard shortcuts, mnemonics, and other forms of navigation, selection, and activation that utilize the keyboard instead of the mouse. (See Appendix A for more on keyboard operations.)

A mnemonic is an underlined letter that typically appears in a menu title, menu item, or the text of a button or other component. The underlined letter reminds users how to activate the equivalent command by pressing the Alt key and the character key that corresponds to the underlined letter. For instance, you could use a mnemonic to give keyboard focus to a text area or to activate a command button. A keyboard shortcut (sometimes called an "accelerator") is a keystroke combination (such as Control-A) that activates a menu item from the keyboard even if the relevant menu is not currently displayed.

Keyboard Focus

The keyboard focus (sometimes called "input focus") designates the active window or component where the user's next keystrokes will take effect. Focus typically moves when users click a component with a pointing device or move to the next component using keyboard equivalents. Either way, users designate the window, or component within a window, that receives input. (Many toolbar buttons are exceptions: for instance, a left-alignment button on a toolbar should not take focus away from the text area where the actual work is taking place.)

  When a window is first opened, assign initial keyboard focus to the component that would normally be used first. Often, this is the component appearing in the upper-left portion of the window. If keyboard focus is not assigned to a component in the active window, the keyboard navigation and control mechanisms cannot be used. The assignment of initial keyboard focus is especially important for people who use only a keyboard to navigate through your application--for instance, those with visual or mobility impairments and many power users.

In the Java look and feel, many components (including command buttons, checkboxes, radio buttons, toggle buttons, list boxes, combo boxes, tabbed panes, editable cells, and tree components) indicate keyboard focus by displaying a rectangular border (blue, in the default color theme).

Figure 69   Keyboard Focus Indicated by Rectangular Border

Rectangular Border Focus

Editable text components, such as text fields, indicate keyboard focus by displaying a blinking bar at the insertion point.

Figure 70   Keyboard Focus Indicated by Blinking Bar at Insertion Point

Blinking Bar Focus

Menus indicate focus with a colored background for menu titles or menu items (blue, in the default color theme).

 Click here to view the corresponding code for Figure 71 (also available on the book's companion CD-ROM).

Figure 71   Keyboard Focus Indicated by Colored Background


Colored Background Focus

Split panes and sliders indicate focus by darkening the zoom buttons and slider indicator (blue, in the default color theme) respectively.

Figure 72   Keyboard Focus Indicated by Drag Texture


Drag Texture Focus

Keyboard Navigation and Activation

Keyboard navigation enables users to move keyboard focus from one user interface component to another using the keyboard. Navigation does not necessarily affect the selection and does not, by itself, cause activation. Keyboard activation, on the other hand, enables users to cause an action by using the keyboard.

In general, pressing the Tab key moves focus through the major components; Shift-Tab moves through the components in the reverse direction. Control-Tab and Control-Shift-Tab work in a similar fashion and are necessary when keyboard focus is in an element that accepts tabs, such as a text area. Arrow keys are often used to move within groups of components--for example, Tab puts focus in a list box and then the arrow keys move focus among the list box items. The Tab key is used to move among checkboxes.

Once an element has focus, pressing the spacebar typically activates its function, such as selecting or deselecting a checkbox. In a list component, pressing Shift-spacebar extends the selection; pressing Control-spacebar toggles the selection state of the current item without affecting any other selections. (Using the up and down arrow keys actually changes the selection in a list component.)

Some components do not need explicit keyboard focus to be operated. For example, users activate the default button in a dialog box by pressing the Enter or Return key. Similarly, scrollbars can be operated from the keyboard using keys such as Page Up and Page Down if focus is anywhere within the scroll pane.

Keyboard navigation can be useful not only for accessibility purposes but also for power users or users who choose alternative input methods like voice input or touch screens.

  Ensure that all application functions are accessible from the keyboard by unplugging the mouse and testing the application's keyboard operations.

  Some of the keyboard operations in the tables in Appendix A are temporarily incomplete or unimplemented. However, the key sequences listed in this appendix should be reserved for future versions of the JFC and the Java 2 platform.

Common keyboard navigation and activation operations are summarized in the following table.


Keyboard Operation Action
Tab 1 Navigates to the next focusable component
Control-Tab 1 Navigates to the next focusable component (works even if the component that previously had focus accepts tabs)
Left arrow Moves focus left one character or component
Right arrow Moves focus right one character or component
Up arrow Moves focus up one line or component
Down arrow Moves focus down one line or component
Page Up Moves up one pane of information
Page Down Moves down one pane of information
Home Moves to the beginning of the data; in a table, moves to the beginning of a row
End Moves to the end of the data; in a table, moves to the last cell in a row
Enter or Return Activates the default command button
Escape Dismisses a menu or dialog box without changes; cancels a drag-and-drop operation in progress
Spacebar Activates the component that has keyboard focus
1 With Shift key, reverses direction


Note — The keyboard operations described in the previous table generally have separate actions for navigation and activation. For the keyboard shortcuts and mnemonics described in the following sections, however, the same action performs both navigation and activation.

Keyboard Shortcuts

Keyboard shortcuts are keystroke combinations that activate a menu item from the keyboard even if the menu for that command is not currently displayed. Keyboard shortcuts usually consist of a modifier key and a character key, like Control-Z, plus a few special keys such as F1 and Delete. Unlike mnemonics, keyboard shortcuts do not post menus; rather, they perform the indicated actions directly. Since all keyboard shortcuts are available at all times, you cannot reuse them as you do mnemonics.

Figure 73 shows an example of keyboard shortcuts and mnemonics on a typical Edit menu.

 Click here to view the corresponding code for Figure 73 (also available on the book's companion CD-ROM).

Figure 73   Edit Menu With Keyboard Shortcuts and Mnemonics


Edit Menu With Shortcuts and Mnemonics

To use a keyboard shortcut in Java look and feel applications, users typically hold down the Control key (and optionally, an additional modifier key, such as Shift) and press the character key that is shown after the menu item; in some cases, they press the single key that corresponds to the shortcut, such as the Delete key in the previous figure. Typing a keyboard shortcut has the same effect as choosing the corresponding menu item. For instance, to undo an action, users can either choose the Undo item from the Edit menu or hold down the Control key and press Z.

  Specify keyboard shortcuts for frequently used menu items to provide an alternative to mouse operation. You do not need to provide a shortcut for all commands.

  Display keyboard shortcuts using the standard abbreviations for key names (such as Ctrl for the Control key), separated by hyphens.

  Be aware of and use the common shortcuts across platforms. If your application doesn't use a particular command, you can use that common shortcut for some other command. However, if a later version of your application is likely to contain the common command, reserve the shortcut so future users won't have to relearn your shortcuts.

  Do not use the Meta key (the Command key on the Macintosh platform) for a keyboard shortcut, except as an alternate for Control. It is not available on some target platforms.

The common keyboard shortcuts (in the order of their use in menus) are summarized in the following table. For an alphabetical listing of the shortcuts in this table, see Common Keyboard Shortcuts.


Sequence Equivalent
Ctrl-N New (File menu)
Ctrl-O Open (File menu)
Ctrl-W Close (File menu)
Ctrl-S Save (File menu)
Ctrl-P Print (File menu)
Ctrl-Z Undo (Edit menu)
Ctrl-Y Redo (Edit menu)
Ctrl-X Cut (Edit menu)
Ctrl-C Copy (Edit menu)
Ctrl-V Paste (Edit menu)
Delete Delete (Edit menu)
Ctrl-F Find (Edit menu)
Ctrl-G Find Again (Edit menu)
Ctrl-A Select All (Edit menu)
Ctrl-H Replace (Edit menu)
Ctrl-B Bold (Format menu)
Ctrl-I Italic (Format menu)
Ctrl-U Underline (Format menu)
Ctrl-L Align Left (Format menu)
Ctrl-E Align Center (Format menu)
Ctrl-R Align Right (Format menu)
F1 Help
Shift-F1 Contextual help
F5 Refresh

  Because each platform has its own standard keyboard shortcuts, ensure that any new keyboard shortcuts you have created are compatible with existing shortcuts on all your target platforms.

  To ease the localization process, place keyboard shortcuts in resource bundles. If it turns out that something needs to be localized because of a situation specific to a particular language, resource bundles facilitate the process.


Mnemonics provide yet another keyboard alternative to the mouse. A mnemonic is an underlined alphanumeric character in a menu title, menu item, or other interface component. It reminds the user how to activate the equivalent command by simultaneously pressing the Alt key and the character key that corresponds to the underlined letter or numeral.

 Click here to view the corresponding code for Figure 74 (also available on the book's companion CD-ROM).

Figure 74   File Menu With Mnemonics and Keyboard Shortcuts


File Menu With Mnemonics and Shortcuts

When keyboard focus is not in a text element, the Alt modifier is not always required. Menus are an example. For instance, to choose the Exit command from the File menu, the user can hold down the Alt key and press F to post the File menu, release the Alt key, and then press X.

Unlike keyboard shortcuts, mnemonics can be reused from one context to another. Once users have displayed a menu with a keyboard sequence, the subsequent keypress can activate a command only from that menu. Hence, users can press Alt-F to display the File menu and then type A to activate the Save As command, or press Alt-E to display the Edit menu, and then type A to activate the Select All command.

You should provide mnemonics for components within the dialog boxes in your application. In dialog boxes, users must press a modifier key to activate the associated component. For instance, within a dialog box, you might have a mnemonic for the Help button. Once keyboard focus is within the dialog box, users press Alt and then H to activate the Help button.

  Provide a mnemonic for all menu titles, menu items, and dialog box components in your application.

  Do not associate mnemonics with the default button or the Cancel button in a dialog box. Use Enter or Return for the default button and Escape for the Cancel button instead.

  Choose mnemonics that avoid conflicts within a context. For instance, you should not use the letter P as the mnemonic for both the Print and Page Setup commands in the File menu.

  Ensure that the mnemonics associated with menu titles are not reused in any context in which the menus are active.

  When you assign mnemonics, follow these guidelines in the specified order.


  2. Use common mnemonics as they appear in Table 9. (For an alphabetical listing of the mnemonics, see Table 13.)


  3. If the mnemonic does not appear in the table of common mnemonics, choose the first letter of the menu item. (For instance, choose J for Justify.)


  4. If the first letter of the menu item conflicts with the mnemonics of other items, choose a prominent consonant. (For instance, the letter S may have already been designated as the mnemonic for the Style command. Therefore, choose the letter Z as the mnemonic for the Size command.)


  5. If the first letter of the menu item and the prominent consonant conflict with those of other menu items, choose a prominent vowel.

  Place mnemonics in resource bundles to facilitate the localization process.


Menu Titles Menu Items
File New, Open, Close, Save, S ave As, Page Set up, Print, E xit
Edit Undo, Redo, Cu t, Copy, Paste, Delete, Find, Fi nd Again, Select All
Fo rmat Font, Style, Si ze, Bold, Italic, Underline, Align Left, Align Center, Align Right
View Lar ge Icons, S mall Icons, List, Details, Sort By, Filter, Zoom In, Zoom Out, Refresh
Help Contents, Tutorial, Index, Search, About Application

  Enable users to use the Tab key to navigate to components without their own text or labels. For instance, a text field might be dependent on a checkbox or a radio button, as shown in the following figure.

Figure 75   Navigating to a Component Without Associated Text


Component Without Associated Text

  Mnemonics vary by locale, so use letters that occur in the localized strings. However, for nonalphabetic languages, use the English mnemonic at the trailing edge of the string. For an example, see Figure 18.

  The setMnemonic method can be used to specify mnemonics for buttons, checkboxes, radio buttons, toggle buttons, and menu titles. For components such as text fields, list boxes, and combo boxes that do not have text of their own, mnemonics can be specified on associated labels. The setDisplayedMnemonic method can be used for labels, and the setMnemonic method for menu items. The labelFor property is used to associate the label and its mnemonic with the appropriate component.

  The Java language underlines the first instance of a letter that appears in the string regardless of whether that instance of the letter led the designer to choose it as the mnemonic. Hence, it would display the mnemonic for the Save As command as S ave As, not Save As.

Java Look and Feel Design Guidelines, second edition.
Copyright 2001. Sun Microsystems, Inc. All Rights Reserved.
Previous Next Contents/Index/Search
Table 9   Common Mnemonics (Organized by Menu)
Table 8  Common Keyboard Shortcuts (Organized by Menus) 
Table 7   Common Navigation and Activation Keys 
Left Curve
Java SDKs and Tools
Right Curve
Left Curve
Java Resources
Right Curve
JavaOne Banner
Java 8 banner (182)