Home > Articles > Programming > Java

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Keystrokes and Actions

When a table component has the focus, the keys that you type are sent to that component. For example, you can press the arrow keys to choose an appropriate focused cell. And, under the Java look and feel, pressing F2 allows you to start editing the contents of the focused cell. Have you ever wondered how the table component associates keys with different operations? The answer to that question is that the table component contains a mapping of Keystroke objects to Action objects. Basically, a Keystroke object identifies a key that you would press on the keyboard, along with modifier information (such as whether Ctrl must be pressed simultaneously with that key), and an Action object is created from a class that directly or indirectly (by way of a superclass) implements the Action interface. Such an object has an actionPerformed(ActionEvent e) method that is called to carry out the action. Table 9 presents a list of Keystroke and Action String names as they appear in BasicLookAndFeel. As I will show you in the next article, you can build on Table 9 by creating your own Actions and mapping them to appropriate Keystrokes.

Table 9 Keystroke and Action Name Strings

Keystroke String

Action Name String

Ctrl+C

copy

Ctrl+V

paste

Ctrl+X

cut

COPY

copy

PASTE

paste

CUT

cut

RIGHT

selectNextColumn

KP_RIGHT

selectNextColumn

LEFT

selectPreviousColumn

KP_LEFT

selectPreviousColumn

DOWN

selectNextRow

KP_DOWN

selectNextRow

UP

selectPreviousRow

KP_UP

selectPreviousRow

Shift+RIGHT

selectNextColumnExtendSelection

Shift+KP_RIGHT

selectNextColumnExtendSelection

Shift+LEFT selectPreviousColumnExtendSelection

 

Shift+KP_LEFT

selectPreviousColumnExtendSelection

Shift+DOWN

selectNextRowExtendSelection

Shift+KP_DOWN

selectNextRowExtendSelection

Shift+UP

selectPreviousRowExtendSelection

Shift+KP_UP

selectPreviousRowExtendSelection

PAGE_UP

scrollUpChangeSelection

PAGE_DOWN

scrollDownChangeSelection

HOME

selectFirstColumn

END

selectLastColumn

Shift+PAGE_UP

scrollUpExtendSelection

Shift+PAGE_DOWN

scrollDownExtendSelection

Shift+HOME

selectFirstColumnExtendSelection

Shift+END

selectLastColumnExtendSelection

Ctrl+PAGE_UP

scrollLeftChangeSelection

Ctrl+PAGE_DOWN

scrollRightChangeSelection

Ctrl+HOME

selectFirstRow

Ctrl+END

selectLastRow

Ctrl+Shift+PAGE_UP

scrollRightExtendSelection

Ctrl+Shift+PAGE_DOWN

scrollLeftExtendSelection

Ctrl+Shift+HOME

selectFirstRowExtendSelection

Ctrl+Shift+END

selectLastRowExtendSelection

TAB

selectNextColumnCell

Shift+TAB

selectPreviousColumnCell

ENTER

selectNextRowCell

Shift+ENTER

selectPreviousRowCell

Ctrl+A

selectAll

ESCAPE

cancel

F2

startEditing


When the table component has the focus and a key (or combination of keys) is pressed, JTable's processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) method is eventually called. That method starts by executing boolean retVal = super.processKeyBinding (ks, e, condition, pressed); (which is found in JComponent). The method checks to see if ks is located in the component's input map (that is, a set of mappings from Keystroke objects to Action names). If found, the resulting Action name is checked against the component's Action map (that is, a set of mappings from Action names to Action objects) to see if that map contains an Action object that associates with the Action name. If so, SwingUtilities' notifyAction(Action action, KeyStroke ks, KeyEvent event, Object sender, int modifiers) method is called to create an ActionEvent object and call the Action's actionPerformed(ActionEvent e) method. Assuming that the actionPerformed(ActionEvent e) method is called, retVal contains a value that causes processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) to exit. Otherwise, that method automatically starts an editor for the focused cell.

  • + Share This
  • 🔖 Save To Your Account