Move these statements into the constructor of your class d
KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_P, 0, false);
area.registerKeyboardAction(listener, ks, JComponent.WHEN_FOCUSED);
so that the KeyStroke
is registered with the JTextArea
area
Also there's no need to create another (AWT) popup menu in the listener - re-use menu
declared at class level.
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
int pos = area.getCaretPosition();
try {
Rectangle r = area.modelToView(pos);
menu.show(area, r.x, r.y);
} catch (BadLocationException ex) {
System.out.print(ex.toString());
}
}
};
Aside: Using Java Naming Conventions classes start with an uppercase letter, for example PopupTest
rather than d