Java Program to paste clipboard text to JTextArea

Java 8Object Oriented ProgrammingProgramming

To paste clopboard text to JTextArea, use tha paste () method. Let’s say the following is our text area −

JTextArea textArea = new JTextArea("");

Now, paste the clipboard text −

textArea.paste();

Note − Let’s say our clipboard text is “This is clipboard text that got inserted in the text area”.

The following is an example to paste clipboard text to JTextArea −

Example

package my;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.*;
public class SwingDemo {
   SwingDemo() {
      JFrame frame = new JFrame("Demo");
      JTextArea textArea = new JTextArea("");
      Container c = frame.getContentPane();
      c.setLayout(new GridLayout(0, 2));
      c.add(textArea);
      // paste clipboard text
      textArea.paste();
      frame.add(textArea);
      frame.setSize(550,300);
      frame.setLayout(new GridLayout(2, 2));
      frame.setVisible(true);
   }
   public static void main(String args[]) {
      new SwingDemo ();
   }
}

Output

raja
Published on 31-May-2019 12:37:52
Advertisements