显示弹出窗口,
如果特定标记不正确,我想显示显示所有可能值的弹出窗口。键入空格键时触发此事件。它工作正常。一个错误是,无论光标在哪里,弹出窗口都会弹出,我只想在正确的标记应该出现的位置显示弹出窗口。
String str = Australia Canberra Dollar
String tokens = str.split("\\s+");
private void editorTextAreaKeyTyped(java.awt.event.KeyEvent evt) {
if ((evt.getKeyChar() == KeyEvent.VK_SPACE)) {
if(!tokens[0].equals("Australia")){
showPopup(editor, menu);
}
}
}
public void showPopUpMenu(JTextArea jTextArea, List menuItems) {
JPopupMenu popup = new JPopupMenu();
Point point = getPoint(jTextArea);
Caret caret = getCaret(jTextArea);
JMenuItem menuItem;
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextArea.insert(e.getActionCommand(), jTextArea.getCaret().getDot());
}
};
for (Object mi : menuItems) {
menuItem = new JMenuItem(mi.toString());
menuItem.addActionListener(al);
popup.add(menuItem);
}
popup.show(jTextArea, point.x, point.y);
}