JPopupMenuのJMenuItemsのショートカットが必要です。以下のコードでは、ショートカットが機能していません。
import java.awt.event.*;
import javax.swing.*;
import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
public class test3 implements ActionListener {
JPopupMenu pop;
void gui() {
JFrame f = new JFrame();
pop = new JPopupMenu();
JMenuItem it1 = new JMenuItem("new");
JMenuItem it2 = new JMenuItem("old");
it1.setAccelerator(KeyStroke.getKeyStroke('N', CTRL_DOWN_MASK));
it2.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
it1.addActionListener(this);
it2.addActionListener(this);
pop.add(it1);
pop.add(it2);
JTextArea ta = new JTextArea(20, 40);
ta.add(pop);
ta.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
if (me.isPopupTrigger()) {
pop.show(me.getComponent(), me.getX(), me.getY());
}
}
public void mouseReleased(MouseEvent me) {
if (me.isPopupTrigger()) {
pop.show(me.getComponent(), me.getX(), me.getY());
}
}
});
f.add(ta);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("new")) {
System.out.println("new");
} else if (ae.getActionCommand().equals("old")) {
System.out.println("old");
}
}
public static void main(String[] argv) {
test3 t = new test3();
t.gui();
}
}