Example usage for javax.swing JTextField replaceSelection

List of usage examples for javax.swing JTextField replaceSelection

Introduction

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

Prototype

public void replaceSelection(String content) 

Source Link

Document

Replaces the currently selected content with new content represented by the given string.

Usage

From source file:dnd.TextTransferHandler.java

/**
 * Perform the actual import.  This method supports both drag and
 * drop and cut/copy/paste./*  ww  w .j  a  v  a 2s. com*/
 */
public boolean importData(TransferHandler.TransferSupport support) {
    //If we can't handle the import, bail now.
    if (!canImport(support)) {
        return false;
    }

    //Fetch the data -- bail if this fails
    String data;
    try {
        data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
    } catch (UnsupportedFlavorException e) {
        return false;
    } catch (java.io.IOException e) {
        return false;
    }

    JTextField tc = (JTextField) support.getComponent();
    tc.replaceSelection(data);
    return true;
}

From source file:TextCutPaste.java

/**
 * Perform the actual import. This method supports both drag and drop and
 * cut/copy/paste./*from  w w  w. j a  va2s .  c  om*/
 */
public boolean importData(TransferHandler.TransferSupport support) {
    // If we can't handle the import, bail now.
    if (!canImport(support)) {
        return false;
    }

    // Fetch the data -- bail if this fails
    String data;
    try {
        data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
    } catch (UnsupportedFlavorException e) {
        return false;
    } catch (java.io.IOException e) {
        return false;
    }

    JTextField tc = (JTextField) support.getComponent();
    tc.replaceSelection(data);
    return true;
}