Example usage for javax.swing JTextField cut

List of usage examples for javax.swing JTextField cut

Introduction

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

Prototype

public void cut() 

Source Link

Document

Transfers the currently selected range in the associated text model to the system clipboard, removing the contents from the 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 w  w w .  j  a  v a2s  .  com*/
    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);
}