4

I want to call a function when the user pastes text in my JTextArea. Is there any event generated when the text is pasted to the JTextArea and which listener can I use to trigger my function on this event?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Muhammad Omer
  • 543
  • 1
  • 8
  • 15

3 Answers3

8

One possible solution (and I hope some one has a better one) would be to replace the key binding Action responsible for actually performing the paste operation.

Now, before you do this, the default paste operation is not trivial, instead, I would replace the default paste Action with a proxy, which could call the original, but would allow you to intercept the operation, but not have to re-implement the functionality yourself, for example...

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 would simply need to look up 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, this won't tell you if the operation failed or succeeded or what was actually pasted. For that, you could use a DocumentListener, registered before you call the default Action which could record the changes to the document. Obviously, you'd want to deregister this after the default action ;)...

Now, equally, you could just override the paste method of the JTextArea, which equates to about the same thing, but, the first option would be more portable...

As an idea...

Take a look at How to Use Actions and How to Use Key Bindings for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    +1, You might want to extend TextAction instead of AbstractAction so the ProxyAction has access to the text area that generated the paste event. – camickr Aug 13 '14 at 01:07
1

you can have something like below, whenever you paste something in the textarea, then 'Pasted!' is printed out on your console. It prints only on paste !

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);
    }
}
user3487063
  • 3,672
  • 1
  • 17
  • 24
  • 1
    But how do you tell the difference between the user typing something and the pasting something, the `DocumentListener` is to abstract – MadProgrammer Aug 13 '14 at 00:14
  • But why use a `KeyListener` when a key binding already exists? Your last answer was forgivable ignorance, this is just blatantly a bad idea... – MadProgrammer Aug 13 '14 at 06:14
0

You can also check out Wrapping Actions which is basically the same suggestion as MadProgrammer except that the WrapperAction will delegate all the methods of the Action to the original Action. This will allow you to pick up the text and Icons associated with the original Action in case you ever want to add your custom Action to a JMenuItem or JButton.

camickr
  • 321,443
  • 19
  • 166
  • 288