这真让我费解。
我有一个JTextComponent
,我为它添加了一个使用JPopupMenu和DefaultEditorKit.Cut\Copy\PasteAction()
右键单击剪切\复制\粘贴菜单。
JMenuItem cutItem = new JMenuItem(new DefaultEditorKit.CutAction());
JMenuItem copyItem = new JMenuItem(new DefaultEditorKit.CopyAction());
JMenuItem pasteItem = new JMenuItem(new DefaultEditorKit.PasteAction());
对于每个操作,我都添加了一个动作侦听器,它捕获JTextComponent的文本,我想在函数中使用它。
final ActionListener textFieldListener = new ActionListener() {
@Override public void actionPerformed(ActionEvent e){someGlobalFunction(textComponent.getText());
}
};
..。
cutItem.addActionListener(textFieldListener );
copyItem.addActionListener(textFieldListener );
pasteItem.addActionListener(textFieldListener );
但是,我唯一能得到的文本是字符串,它是在我将\ can剪切到组件之前的,而不是在后面。
有什么明显的解决办法吗?
发布于 2010-06-02 19:36:52
将代码包装在actionPerformed()方法中的SwingUtilities.invokeLater(.)中,这将将代码添加到EDT的末尾,因此它应该在剪切/复制/粘贴命令之后执行。
发布于 2010-06-02 19:32:50
这是因为你不听你的文本字段,你听菜单:-)
将侦听器放在文本字段,或文本字段的文档上,或者FilterDocument,甚至您自己的文档上。
https://stackoverflow.com/questions/2960703
复制相似问题