Java Code Examples for javafx.scene.input.Clipboard#getString()

The following examples show how to use javafx.scene.input.Clipboard#getString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UIMenuItem.java    From tcMenu with Apache License 2.0 5 votes vote down vote up
public boolean handlePaste() {
    TextField focusedTF = getFocusedTextField();
    if(focusedTF == null) return false;

    Clipboard systemClipboard = Clipboard.getSystemClipboard();
    if (!systemClipboard.hasContent(DataFormat.PLAIN_TEXT)) {
        return true;
    }

    String clipboardText = systemClipboard.getString();

    IndexRange range = focusedTF.getSelection();

    String origText = focusedTF.getText();

    int endPos;
    String updatedText;
    String firstPart = origText.substring(0, range.getStart());
    String lastPart = origText.substring(range.getEnd());

    updatedText = firstPart + clipboardText + lastPart;

    if (range.getStart() == range.getEnd()) {
        endPos = range.getEnd() + clipboardText.length();
    } else {
        endPos = range.getStart() + clipboardText.length();
    }

    focusedTF.setText(updatedText);
    focusedTF.positionCaret(endPos);
    return true;
}
 
Example 2
Source File: SystemTools.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static String fetchTextInClipboard(boolean clear) {
    Clipboard clipboard = Clipboard.getSystemClipboard();
    if (!clipboard.hasString()) {
        return null;
    }
    String text = clipboard.getString();
    if (clear) {
        clipboard.clear();
    }
    return text;
}
 
Example 3
Source File: BrowsableField.java    From pdfsam with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void paste() {
    Clipboard clipboard = Clipboard.getSystemClipboard();
    if (clipboard.hasString()) {
        String text = clipboard.getString();
        if (text.length() > 2 && text.charAt(0) == '"' && text.charAt(text.length() - 1) == '"') {
            replaceSelection(text.substring(1, text.length() - 1));
        } else {
            super.paste();
        }
    }
}
 
Example 4
Source File: ClipboardActions.java    From RichTextFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Inserts the content from the clipboard into this text-editing area,
 * replacing the current selection. If there is no selection, the content
 * from the clipboard is inserted at the current caret position.
 */
default void paste() {
    Clipboard clipboard = Clipboard.getSystemClipboard();

    if(getStyleCodecs().isPresent()) {
        Tuple2<Codec<PS>, Codec<StyledSegment<SEG, S>>> codecs = getStyleCodecs().get();
        Codec<StyledDocument<PS, SEG, S>> codec = ReadOnlyStyledDocument.codec(codecs._1, codecs._2, getSegOps());
        DataFormat format = dataFormat(codec.getName());
        if(clipboard.hasContent(format)) {
            byte[] bytes = (byte[]) clipboard.getContent(format);
            ByteArrayInputStream is = new ByteArrayInputStream(bytes);
            DataInputStream dis = new DataInputStream(is);
            StyledDocument<PS, SEG, S> doc = null;
            try {
                doc = codec.decode(dis);
            } catch (IOException e) {
                System.err.println("Codec error: Failed to decode '" + codec.getName() + "':");
                e.printStackTrace();
            }
            if(doc != null) {
                replaceSelection(doc);
                return;
            }
        }
    }

    if (clipboard.hasString()) {
        String text = clipboard.getString();
        if (text != null) {
            replaceSelection(text);
        }
    }
}
 
Example 5
Source File: GuiTest.java    From VocabHunter with Apache License 2.0 4 votes vote down vote up
private String collectCurrentClipboardText() {
    Clipboard clipboard = Clipboard.getSystemClipboard();

    return clipboard.getString();
}