Example usage for javax.swing JTextField getSelectionStart

List of usage examples for javax.swing JTextField getSelectionStart

Introduction

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

Prototype

@Transient
public int getSelectionStart() 

Source Link

Document

Returns the selected text's start position.

Usage

From source file:TextCutPaste.java

/**
 * Bundle up the data for export.// ww w .  j a  va2 s . c  om
 */
protected Transferable createTransferable(JComponent c) {
    JTextField source = (JTextField) c;
    int start = source.getSelectionStart();
    int end = source.getSelectionEnd();
    Document doc = source.getDocument();
    if (start == end) {
        return null;
    }
    try {
        p0 = doc.createPosition(start);
        p1 = doc.createPosition(end);
    } catch (BadLocationException e) {
        System.out.println("Can't create position - unable to remove text from source.");
    }
    String data = source.getSelectedText();
    return new StringSelection(data);
}