I'm gonna answer my own question. If I've made any errors or misinterpreted or misinformed, I hope to find out from someone. Question in advance: Did I mess anything up below?
Key bindings provide the only way to do some things. For instance...
I needed a keypress to cause the bottom line of a textarea to be displayed regardless of which component has the focus and to then select the contents of the 'main' textbox.
The statements below link the physical F2 keypress to a logical button on the form, whose
action will be defined in a class (named JumpToEndOfOutput
) that extends AbstractAction
, as required by getActionMap
.
txaOutput.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(getKeyStroke("F2"),
"jumpToEndOfOutput");
txaOutput.getActionMap().put("jumpToEndOfOutput", jumpToEndOfOutput);
(note use of WHEN_IN_FOCUSED_WINDOW
, without which F2 can't always do what's needed):
// in constructor for form... make action listener for button
btnJumpToEndOfOutput.addActionListener( new ButtonListener() );
...
// inner class avoids anonymous inner class, for clarity
class ButtonListener implements ActionListener // simulates click of logical form button
{
public void actionPerformed( ActionEvent bp )
{
txaOutput.selectAll();
txtPattern.grabFocus();
txtPattern.select(0, 99);
}
}
...
// back to constructor... make action object to listen for F2 keystroke
JumpToEndOfOutput jumpToEndOfOutput = new JumpToEndOfOutput();
...
// class required for getActionMap
class JumpToEndOfOutput extends AbstractAction // catches physical F2 keystroke
{
public void actionPerformed(ActionEvent e)
{
btnJumpToEndOfOutput.doClick();
}
}
Is there a shorter way to accomplish this?