Example usage for javax.swing JTextField copy

List of usage examples for javax.swing JTextField copy

Introduction

In this page you can find the example usage for javax.swing JTextField copy.

Prototype

public void copy() 

Source Link

Document

Transfers the currently selected range in the associated text model to the system clipboard, leaving the contents in the text model.

Usage

From source file:Main.java

public static void main(String args[]) {
    final JTextField textField = new JTextField(15);
    JButton buttonCut = new JButton("Cut");
    JButton buttonPaste = new JButton("Paste");
    JButton buttonCopy = new JButton("Copy");

    JFrame jfrm = new JFrame("Cut, Copy, and Paste");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(230, 150);/*from  ww  w . j  a  va  2 s  .c  o m*/
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    buttonCut.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent le) {
            textField.cut();
        }
    });

    buttonPaste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent le) {
            textField.paste();
        }
    });

    buttonCopy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent le) {
            textField.copy();
        }
    });

    textField.addCaretListener(new CaretListener() {
        public void caretUpdate(CaretEvent ce) {
            System.out.println("All text: " + textField.getText());
            if (textField.getSelectedText() != null)
                System.out.println("Selected text: " + textField.getSelectedText());
            else
                System.out.println("Selected text: ");
        }
    });

    jfrm.add(textField);
    jfrm.add(buttonCut);
    jfrm.add(buttonPaste);
    jfrm.add(buttonCopy);
    jfrm.setVisible(true);
}