Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;

import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Main {
    /**
     * Copy a JTextField text (jTextField.getText()) to clipborad
     *
     * @param jTextField
     */
    public static void copyToClipboard(JTextField jTextField) {
        copyToClipboard(jTextField.getText());
    }

    /**
     * Copy a JTextArea text (jTextArea.getText()) to clipborad
     *
     * @param jTextArea
     */
    public static void copyToClipboard(JTextArea jTextArea) {
        copyToClipboard(jTextArea.getText());
    }

    /**
     * Copy a String to clipborad
     *
     * @param string
     */
    public static void copyToClipboard(String string) {
        StringSelection stringSelection = new StringSelection(string);
        Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
        clpbrd.setContents(stringSelection, null);
    }
}