Listen for JTextArea paste events
One possible solution (and hopefully someone better) is to replace the key binding Action
responsible for actually performing the insert operation.
Now, before you do that, the default insert operation is not trivial, instead I would replace the default Action
proxy paste that the original could call, but would allow you to intercept the operation, but no need to re-implement the functionality yourself, like ...
public class ProxyAction extends AbstractAction {
private Action action;
public ProxyAction(Action action) {
this.action = action;
}
@Override
public void actionPerformed(ActionEvent e) {
action.actionPerformed(e);
System.out.println("Paste Occured...");
}
}
Then you just need to find the default Action
and replace it ...
JTextArea ta = new JTextArea(10, 10);
Action action = ta.getActionMap().get("paste-from-clipboard");
ta.getActionMap().put("paste-from-clipboard", new ProxyAction(action));
The problem here is that it won't tell you if the operation was unsuccessful or successful, or what was actually inserted. For this you can use DocumentListener
registered before you call the default Action
, which can record changes to the document. Obviously you will want to unregister after the default action;) ...
Now, equally, you can just override a method paste
JTextArea
that equates to the same thing, but the first option will be more portable ...
As an idea ...
Take a look at How to Use Actions and How To Use Key Bindings for more details
source to share
you might have something like below, whenever you paste something into the textbox, then "Paste!" printed to the console. It only prints on insert !
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class TextAreaDemo extends JFrame {
JTextArea _resultArea = new JTextArea(6, 20);
public TextAreaDemo() {
_resultArea.setText("");
JScrollPane scrollingArea = new JScrollPane(_resultArea);
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scrollingArea, BorderLayout.CENTER);
this.setContentPane(content);
this.setTitle("TextAreaDemo B");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
_resultArea.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if ((e.getKeyCode() == KeyEvent.VK_V) && ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {
System.out.println("Pasted!");
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
}
public static void main(String[] args) {
JFrame win = new TextAreaDemo();
win.setVisible(true);
}
}
source to share
You can also check out Wrapping Actions , which is basically the same suggestion as MadProgrammer, except it WrapperAction
will pass all the Action methods back to the original action. This will allow you to select the text and icons associated with the original action if you want to add your custom action to the JMenuItem or JButton.
source to share