开发者

Getting an action after text is pasted into a JTextComponent (Java)

This really puzzles me.

I have a JTextComponent for which I've added a right-click cut\copy\paste menu using a JPopupMenu and 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());

For each action I've added an action listener which grabs the JTextComponent's text, which I want to use in a function.

final ActionListener textFieldListener = new ActionList开发者_如何转开发ener() {
@Override public void actionPerformed(ActionEvent e){someGlobalFunction(textComponent.getText());
}
}; 

...

cutItem.addActionListener(textFieldListener );
copyItem.addActionListener(textFieldListener );
pasteItem.addActionListener(textFieldListener );

However, the only text I can get hold on is the String which it was before I cut\paste into the component, not after.

Is there any obvious solution for this?


Wrap the code in the actionPerformed() method in a SwingUtilities.invokeLater(...), This will add the code to then end of the EDT so it should execute after the cut/copy/paste commands.


It is because you don't listen your text field, you listen the menu :-)

Put a listener on your text field, or on the document of your text field, or perhaps a FilterDocument, or even your own document.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜