Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get readable text only from clipboard

Tags:

java

clipboard

People also ask

How to get text from clipboard java?

"To read data from the clipboard, a program calls the Transferable. getTransferData() method. If the data is represented by a DataFlavor that doesn't correspond to a Java class (for example, plainTextFlavor), getTransferData() returns an InputStream for you to read the data from."

How do I find the clipboard content?

To get to your clipboard history at any time, press Windows logo key + V. From the clipboard history, you can paste and pin frequently used items by choosing an individual item from your clipboard menu. Pinning an item keeps it from being removed from the clipboard history to make room for new items.


import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

String data = (String) Toolkit.getDefaultToolkit()
                .getSystemClipboard().getData(DataFlavor.stringFlavor); 

with the getData() Method and the stringFlavor you should get plain text from the clipboard.

If there are weird text in the clipboard, I think, this should be a problem of the program which puts the data in the clipboard.


You can use following method getting clipboard text in Java:

public String getClipBoard(){
    try {
        return (String)Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
    } catch (HeadlessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();            
    } catch (UnsupportedFlavorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();            
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "";
}