Java Code Examples for java.awt.datatransfer.Clipboard#getData()

The following examples show how to use java.awt.datatransfer.Clipboard#getData() . 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: bug8059739.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void runTest() throws Exception {
    String testString = "my string";
    JTextField tf = new JTextField(testString);
    tf.selectAll();
    Clipboard clipboard = new Clipboard("clip");
    tf.getTransferHandler().exportToClipboard(tf, clipboard, TransferHandler.COPY);
    DataFlavor[] dfs = clipboard.getAvailableDataFlavors();
    for (DataFlavor df: dfs) {
        String charset = df.getParameter("charset");
        if (InputStream.class.isAssignableFrom(df.getRepresentationClass()) &&
                charset != null) {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (InputStream) clipboard.getData(df), charset));
            String s = br.readLine();
            System.out.println("Content: '" + s + "'");
            passed &= s.contains(testString);
        }
    }
}
 
Example 2
Source File: PasteAction.java    From ganttproject with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void actionPerformed(ActionEvent evt) {
  if (calledFromAppleScreenMenu(evt)) {
    return;
  }
  ChartSelection selection = myViewmanager.getSelectedArtefacts();
  if (!selection.isEmpty()) {
    pasteInternalFlavor(selection);
    return;
  }
  Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  if (clipboard.isDataFlavorAvailable(GPTransferable.EXTERNAL_DOCUMENT_FLAVOR)) {
    try {
      Object data = clipboard.getData(GPTransferable.EXTERNAL_DOCUMENT_FLAVOR);
      if (data instanceof InputStream == false) {
        return;
      }
      pasteExternalDocument((InputStream) data);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
 
Example 3
Source File: bug8059739.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void runTest() throws Exception {
    String testString = "my string";
    JTextField tf = new JTextField(testString);
    tf.selectAll();
    Clipboard clipboard = new Clipboard("clip");
    tf.getTransferHandler().exportToClipboard(tf, clipboard, TransferHandler.COPY);
    DataFlavor[] dfs = clipboard.getAvailableDataFlavors();
    for (DataFlavor df: dfs) {
        String charset = df.getParameter("charset");
        if (InputStream.class.isAssignableFrom(df.getRepresentationClass()) &&
                charset != null) {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (InputStream) clipboard.getData(df), charset));
            String s = br.readLine();
            System.out.println("Content: '" + s + "'");
            passed &= s.contains(testString);
        }
    }
}
 
Example 4
Source File: bug8059739.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static void runTest() throws Exception {
    String testString = "my string";
    JTextField tf = new JTextField(testString);
    tf.selectAll();
    Clipboard clipboard = new Clipboard("clip");
    tf.getTransferHandler().exportToClipboard(tf, clipboard, TransferHandler.COPY);
    DataFlavor[] dfs = clipboard.getAvailableDataFlavors();
    for (DataFlavor df: dfs) {
        String charset = df.getParameter("charset");
        if (InputStream.class.isAssignableFrom(df.getRepresentationClass()) &&
                charset != null) {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (InputStream) clipboard.getData(df), charset));
            String s = br.readLine();
            System.out.println("Content: '" + s + "'");
            passed &= s.contains(testString);
        }
    }
}
 
Example 5
Source File: bug8059739.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void runTest() throws Exception {
    String testString = "my string";
    JTextField tf = new JTextField(testString);
    tf.selectAll();
    Clipboard clipboard = new Clipboard("clip");
    tf.getTransferHandler().exportToClipboard(tf, clipboard, TransferHandler.COPY);
    DataFlavor[] dfs = clipboard.getAvailableDataFlavors();
    for (DataFlavor df: dfs) {
        String charset = df.getParameter("charset");
        if (InputStream.class.isAssignableFrom(df.getRepresentationClass()) &&
                charset != null) {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (InputStream) clipboard.getData(df), charset));
            String s = br.readLine();
            System.out.println("Content: '" + s + "'");
            passed &= s.contains(testString);
        }
    }
}
 
Example 6
Source File: bug8059739.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void runTest() throws Exception {
    String testString = "my string";
    JTextField tf = new JTextField(testString);
    tf.selectAll();
    Clipboard clipboard = new Clipboard("clip");
    tf.getTransferHandler().exportToClipboard(tf, clipboard, TransferHandler.COPY);
    DataFlavor[] dfs = clipboard.getAvailableDataFlavors();
    for (DataFlavor df: dfs) {
        String charset = df.getParameter("charset");
        if (InputStream.class.isAssignableFrom(df.getRepresentationClass()) &&
                charset != null) {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (InputStream) clipboard.getData(df), charset));
            String s = br.readLine();
            System.out.println("Content: '" + s + "'");
            passed &= s.contains(testString);
        }
    }
}
 
Example 7
Source File: bug8059739.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static void runTest() throws Exception {
    String testString = "my string";
    JTextField tf = new JTextField(testString);
    tf.selectAll();
    Clipboard clipboard = new Clipboard("clip");
    tf.getTransferHandler().exportToClipboard(tf, clipboard, TransferHandler.COPY);
    DataFlavor[] dfs = clipboard.getAvailableDataFlavors();
    for (DataFlavor df: dfs) {
        String charset = df.getParameter("charset");
        if (InputStream.class.isAssignableFrom(df.getRepresentationClass()) &&
                charset != null) {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (InputStream) clipboard.getData(df), charset));
            String s = br.readLine();
            System.out.println("Content: '" + s + "'");
            passed &= s.contains(testString);
        }
    }
}
 
Example 8
Source File: bug8059739.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static void runTest() throws Exception {
    String testString = "my string";
    JTextField tf = new JTextField(testString);
    tf.selectAll();
    Clipboard clipboard = new Clipboard("clip");
    tf.getTransferHandler().exportToClipboard(tf, clipboard, TransferHandler.COPY);
    DataFlavor[] dfs = clipboard.getAvailableDataFlavors();
    for (DataFlavor df: dfs) {
        String charset = df.getParameter("charset");
        if (InputStream.class.isAssignableFrom(df.getRepresentationClass()) &&
                charset != null) {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (InputStream) clipboard.getData(df), charset));
            String s = br.readLine();
            System.out.println("Content: '" + s + "'");
            passed &= s.contains(testString);
        }
    }
}
 
Example 9
Source File: DataFlavorRemoteTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Clipboard clipboard =
            Toolkit.getDefaultToolkit().getSystemClipboard();
    DataFlavor dataFlavor = new DataFlavor(DataFlavor.javaRemoteObjectMimeType +
            ";class=Hello" );
    Object data = clipboard.getData(dataFlavor);
    System.err.print(((Hello) data).sayHello());
}
 
Example 10
Source File: CropUIController.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
Image getScreenfromClipB() {
    final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    if (clipboard.isDataFlavorAvailable(DataFlavor.imageFlavor)) {
        try {
            return (Image) clipboard.getData(DataFlavor.imageFlavor);
        } catch (UnsupportedFlavorException | IOException ex) {
            Logger.getLogger(CropUIController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return null;
}
 
Example 11
Source File: RobotInput.java    From knife with MIT License 5 votes vote down vote up
public final String getSelectedString(){
		try {
			Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();//获取剪切板
			Transferable origin = clip.getContents(null);//备份之前剪切板的内容

			String selectedString = (String)clip.getData(DataFlavor.stringFlavor);
			System.out.println("复制之前剪切板中的内容:"+selectedString);
			
			inputWithCtrl(KeyEvent.VK_C);
			final String result = (String)clip.getData(DataFlavor.stringFlavor);
			//selectedString = (String)clip.getData(DataFlavor.stringFlavor);
			System.out.println("复制之后剪切板中的内容:"+result);

			clip.setContents(origin, null);//恢复之前剪切板的内容

			selectedString = (String)clip.getData(DataFlavor.stringFlavor);
			System.out.println("恢复之后剪切板中的内容:"+selectedString);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
//		复制之前剪切板中的内容:printStackTrace
//		复制之后剪切板中的内容:null
//		恢复之后剪切板中的内容:printStackTrace
//		printStackTrace//最后的值随着剪切板的恢复而改变了,应该是引用传递的原因。所有需要将复制后的值设置为final。
	}
 
Example 12
Source File: ClipboardUtils.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Retrieves text from the system clipboard.
 *
 * @return string
 */
public static String getClipboardString() throws IOException, UnsupportedFlavorException {
    final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    if (clipboard != null) {
        return (String) clipboard.getData(DataFlavor.stringFlavor);
    }
    return null;
}
 
Example 13
Source File: TextInputBox.java    From FEMultiPlayer-V2 with GNU General Public License v3.0 5 votes vote down vote up
private static String getClipboardContent(){
		Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
		try {
			return (String) cb.getData(DataFlavor.stringFlavor);
		} catch (UnsupportedFlavorException | IOException e) {
			e.printStackTrace();
			return "";
		}
}
 
Example 14
Source File: ChatHelper.java    From MercuryTrade with MIT License 5 votes vote down vote up
private void executeTradeMessage() {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Clipboard clipboard = toolkit.getSystemClipboard();
    try {
        String result = (String) clipboard.getData(DataFlavor.stringFlavor);
        if (result != null && (result.contains("listed for") || result.contains("for my"))) {
            this.gameToFront();
            MercuryStoreCore.blockHotkeySubject.onNext(true);
            robot.keyRelease(KeyEvent.VK_ALT);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);

            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_A);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_A);

            robot.keyPress(KeyEvent.VK_BACK_SPACE);
            robot.keyRelease(KeyEvent.VK_BACK_SPACE);

            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);

            Timer timer = new Timer(300, action -> {
                StringSelection selection = new StringSelection("");
                clipboard.setContents(selection, null);
            });
            timer.setRepeats(false);
            timer.start();

            MercuryStoreCore.blockHotkeySubject.onNext(false);
        }
    } catch (UnsupportedFlavorException | IOException e) {
        MercuryStoreCore.errorHandlerSubject.onNext(new MercuryError(e));
    }
}
 
Example 15
Source File: Utils.java    From Scripts with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Retrieves text from the system clipboard.
 *
 * @return the text contents of the clipboard or an empty string if no text
 *         could be retrieved
 */
public static String getClipboardText() {
	String text = "";
	try {
		final Toolkit toolkit = Toolkit.getDefaultToolkit();
		final Clipboard clipboard = toolkit.getSystemClipboard();
		text = (String) clipboard.getData(DataFlavor.stringFlavor);
	} catch (final Exception e) {
		// if (IJ.debugMode) IJ.handleException(e);
	}
	return text;
}
 
Example 16
Source File: ClipboardUtils.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Retrieves a list of files from the system clipboard.
 *
 * @return file[] list
 */
public static File[] getClipboardFileList() throws IOException, UnsupportedFlavorException {
    final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    if (clipboard != null) {
        final List<File> fileList = (List<File>) clipboard.getData(DataFlavor.javaFileListFlavor);
        return fileList.toArray(new File[fileList.size()]);
    }
    return new File[0];
}
 
Example 17
Source File: GUIFrame.java    From jaamsim with Apache License 2.0 5 votes vote down vote up
public Entity getEntityFromClipboard() {
	Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
	try {
		String name = (String)clpbrd.getData(DataFlavor.stringFlavor);
		return sim.getNamedEntity(name);
	}
	catch (Throwable err) {
		return null;
	}
}
 
Example 18
Source File: Keyboard.java    From jace with GNU General Public License v2.0 5 votes vote down vote up
@InvokableAction(name = "Paste clipboard", alternatives = "paste", category = "Keyboard", notifyOnRelease = false, defaultKeyMapping = {"Ctrl+Shift+V","Shift+Insert"}, consumeKeyEvent = true)
public static void pasteFromClipboard() {
    try {
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        String contents = (String) clip.getData(DataFlavor.stringFlavor);
        if (contents != null && !"".equals(contents)) {
            contents = contents.replaceAll("\\r?\\n|\\r", (char) 0x0d + "");
            pasteBuffer = new StringReader(contents);
        }
    } catch (UnsupportedFlavorException | IOException ex) {
        Logger.getLogger(Keyboard.class
                .getName()).log(Level.SEVERE, null, ex);
    }

}
 
Example 19
Source File: ImageHelper.java    From tess4j with Apache License 2.0 5 votes vote down vote up
/**
 * Gets an image from Clipboard.
 *
 * @return image
 */
public static Image getClipboardImage() {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    try {
        return (Image) clipboard.getData(DataFlavor.imageFlavor);
    } catch (Exception e) {
        return null;
    }
}
 
Example 20
Source File: GuiUtilities.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
public static String getFromClipboard() throws Exception {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    String string = (String) clipboard.getData(DataFlavor.stringFlavor);
    return string;
}