java.awt.datatransfer.Clipboard类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(218)

本文整理了Java中java.awt.datatransfer.Clipboard类的一些代码示例,展示了Clipboard类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Clipboard类的具体详情如下:
包路径:java.awt.datatransfer.Clipboard
类名称:Clipboard

Clipboard介绍

暂无

代码示例

代码示例来源:origin: libgdx/libgdx

@Override
public void setContents (String content) {
  try {
    StringSelection stringSelection = new StringSelection(content);
    java.awt.datatransfer.Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(stringSelection, this);
  } catch (Exception ignored) { // Ignore JDK crashes sorting data flavors.
  }
}

代码示例来源:origin: MovingBlocks/Terasology

@Override
public String getClipboardContentsAsString() {
  return AccessController.doPrivileged(
      (PrivilegedAction<String>) () -> {
        Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
        try {
          if (t != null) {
            return (String) t.getTransferData(DataFlavor.stringFlavor);
          }
        } catch (UnsupportedFlavorException | IOException e) {
          return null;
        }
        return null;
      });
}

代码示例来源:origin: stackoverflow.com

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);

代码示例来源:origin: stackoverflow.com

throws UnsupportedFlavorException, IOException
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection testData;
  testData = new StringSelection( args[0] );
else
  testData = new StringSelection( "Test Data" );
c.setContents(testData, testData);
Transferable t = c.getContents( null );
if ( t.isDataFlavorSupported(DataFlavor.stringFlavor) )
  Object o = t.getTransferData( DataFlavor.stringFlavor );
  String data = (String)t.getTransferData( DataFlavor.stringFlavor );
  System.out.println( "Clipboard contents: " + data );

代码示例来源:origin: stackoverflow.com

try {
  Robot robot = new Robot();
  Dimension screenSize  = Toolkit.getDefaultToolkit().getScreenSize();
  Rectangle screen = new Rectangle( screenSize );
  BufferedImage i = robot.createScreenCapture( screen );
  TransferableImage trans = new TransferableImage( i );
  Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
  c.setContents( trans, this );
  if ( flavor.equals( DataFlavor.imageFlavor ) && i != null ) {
    return i;
  DataFlavor[] flavors = getTransferDataFlavors();
  for ( int i = 0; i < flavors.length; i++ ) {
    if ( flavor.equals( flavors[ i ] ) ) {
      return true;

代码示例来源:origin: skylot/jadx

private void process(MouseEvent e) {
    textComponent.requestFocus();

    boolean enabled = textComponent.isEnabled();
    boolean editable = textComponent.isEditable();
    boolean nonempty = !(textComponent.getText() == null || textComponent.getText().equals(""));
    boolean marked = textComponent.getSelectedText() != null;
    boolean pasteAvailable = Toolkit.getDefaultToolkit().getSystemClipboard()
        .getContents(null).isDataFlavorSupported(DataFlavor.stringFlavor);

    undoAction.setEnabled(enabled && editable && undoManager.canUndo());
    redoAction.setEnabled(enabled && editable && undoManager.canRedo());
    cutAction.setEnabled(enabled && editable && marked);
    copyAction.setEnabled(enabled && marked);
    pasteAction.setEnabled(enabled && editable && pasteAvailable);
    deleteAction.setEnabled(enabled && editable && marked);
    selectAllAction.setEnabled(enabled && nonempty);

    int nx = e.getX();
    if (nx > 500) {
      nx = nx - popup.getSize().width;
    }
    popup.show(e.getComponent(), nx, e.getY() - popup.getSize().height);
  }
}

代码示例来源:origin: jline/jline

Clipboard clipboard;
try { // May throw ugly exception on system without X
  clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable transferable = clipboard.getContents(null);
  Object content = transferable.getTransferData(DataFlavor.plainTextFlavor);
      content = new DataFlavor().getReaderForText(transferable);

代码示例来源:origin: net.java.dev.laf-widget/laf-widget

@Override
  public boolean isEnabled() {
    if (jcomp.isEditable() && jcomp.isEnabled()) {
      Transferable contents = Toolkit.getDefaultToolkit()
          .getSystemClipboard().getContents(this);
      return contents.isDataFlavorSupported(DataFlavor.stringFlavor);
    } else
      return false;
  }
}

代码示例来源:origin: org.owasp.jbrofuzz/jbrofuzz

public void actionPerformed(final ActionEvent e) {
    final Clipboard clipboard = Toolkit.getDefaultToolkit()
    .getSystemClipboard();
    final Transferable clipData = clipboard.getContents(clipboard);
    if (clipData != null) {
      try {
        if (clipData
            .isDataFlavorSupported(DataFlavor.stringFlavor)) {
          ((JTextComponent) databaseBox.getEditor().getEditorComponent()).copy();
        }
      } catch (final Exception e1) {
        Logger.log("Open Location: An error occured while copying",2);
      }
    }
  }
});

代码示例来源:origin: stackoverflow.com

robot.delay(404);
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
DataFlavor[] flavors = cb.getAvailableDataFlavors();
System.out.println("After: ");
for (DataFlavor flavor : flavors) {
  System.out.println(flavor);
  if (flavor.toString().indexOf("java.awt.Image")>0) {
    Object o = cb.getData(flavor);
    Image i = (Image)o;

代码示例来源:origin: org.swinglabs.swingx/swingx-core

/**
 * The paste method has been overloaded to strip off the <html><body> tags
 * This doesn't really work.
 */
@Override
public void paste() {
  Clipboard clipboard = getToolkit().getSystemClipboard();
  Transferable content = clipboard.getContents(this);
  if (content != null) {
    DataFlavor[] flavors = content.getTransferDataFlavors();
    try {
      for (int i = 0; i < flavors.length; i++) {
        if (String.class.equals(flavors[i].getRepresentationClass())) {
          Object data = content.getTransferData(flavors[i]);
          if (flavors[i].isMimeTypeEqual("text/plain")) {
            // This works but we lose all the formatting.
            replaceSelection(data.toString());
            break;
          } 
        }
      }
    } catch (Exception ex) {
      // TODO change to something meaningful - when can this acutally happen?
      LOG.log(Level.FINE, "What can produce a problem with data flavor?", ex);
    }
  }
}

代码示例来源:origin: stackoverflow.com

private static final DataFlavor PLAIN_FLAVOR = new DataFlavor("text/plain", "Plain Flavor");
private static final DataFlavor RTF_FLAVOR = new DataFlavor("text/rtf", "Rich Formatted Text");
  Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
  cb.setContents(p, null);
    if (df.getMimeType().contains("text/rtf")){

代码示例来源:origin: tinyMediaManager/tinyMediaManager

@Override
 public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
  JTextComponent jtc = (JTextComponent) popupMenu.getInvoker();
  boolean enableCutCopy = (jtc.getSelectionEnd() - jtc.getSelectionStart()) > 0;
  boolean enablePaste = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null) != null;
  mntmCut.setEnabled(enableCutCopy);
  mntmCopy.setEnabled(enableCutCopy);
  mntmPaste.setEnabled(enablePaste);
 }
});

代码示例来源:origin: stackoverflow.com

package test;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import javax.swing.*;

public class GetStringFromClipboard {

  public static void main(String[] args) throws Exception {

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Clipboard clipboard = toolkit.getSystemClipboard();
    String result = (String) clipboard.getData(DataFlavor.stringFlavor);
    JTextField tf= new JTextField(20);
    tf.setText(result);
  }

}

代码示例来源:origin: mabe02/lanterna

private void pasteSelectionContent() {
  try {
    Clipboard systemSelection = Toolkit.getDefaultToolkit().getSystemSelection();
    if(systemSelection != null) {
      injectStringAsKeyStrokes((String) systemSelection.getData(DataFlavor.stringFlavor));
    }
  }
  catch(Exception ignore) {
  }
}

代码示例来源:origin: vsch/flexmark-java

public static Image getImageFromClipboard() {
  Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
  return getImageFromTransferable(transferable);
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_core

public static JMeterTreeNode[] getCopiedNodes() {
  Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
  if (clipboard.isDataFlavorAvailable(JMeterTreeNodeTransferable.JMETER_TREE_NODE_ARRAY_DATA_FLAVOR)) {
    try {
      return (JMeterTreeNode[]) clipboard.getData(JMeterTreeNodeTransferable.JMETER_TREE_NODE_ARRAY_DATA_FLAVOR);
    } catch (Exception ex) {
      log.error("Clipboard node read error: {}", ex.getMessage(), ex);
      JOptionPane.showMessageDialog(GuiPackage.getInstance().getMainFrame(), 
          JMeterUtils.getResString("clipboard_node_read_error")+":\n" + ex.getLocalizedMessage(),  //$NON-NLS-1$  //$NON-NLS-2$
          JMeterUtils.getResString("error_title"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$
    }
  }
  return null;
}

代码示例来源:origin: scruel/ClipIt

public static Clipboard getClipboard() {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    while (true) {
      Throwable throwable = null;
      try {
        clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor);
      } catch (IllegalStateException e) {
        throwable = e;
      }
      if (throwable == null) {
        break;
      }
    }
    return clipboard;
  }
}

代码示例来源:origin: looly/hutool

/**
 * 获取剪贴板内容
 * 
 * @param flavor 数据元信息,标识数据类型
 * @param owner 所有者
 * @return 剪贴板内容,类型根据flavor不同而不同
 */
public static Object get(DataFlavor flavor, ClipboardOwner owner) {
  final Transferable content = getClipboard().getContents(null);
  if (null != content && content.isDataFlavorSupported(flavor)) {
    try {
      return content.getTransferData(flavor);
    } catch (UnsupportedFlavorException | IOException e) {
      throw new UtilException(e);
    }
  }
  return null;
}

代码示例来源:origin: IanDarwin/javasrc

public void actionPerformed(ActionEvent evt) {
  String cmd = evt.getActionCommand();
  if (cmd.equals("copy")) { 
    // Implement Copy operation
    String srcData = srcText.getText();
    if (srcData != null) {
      StringSelection contents = new StringSelection(srcData);
      clipboard.setContents(contents, this);
      pasteButton.setEnabled(true);
    }
  } else if (cmd.equals("paste")) {
    // Implement Paste operation
    Transferable content = clipboard.getContents(this);
    if (content != null) {
      try {
        String dstData = (String)content.getTransferData(
                      DataFlavor.stringFlavor);
        dstText.append(dstData);
      } catch (Exception e) {
        System.out.println("Couldn't get contents in format: "+
            DataFlavor.stringFlavor.getHumanPresentableName()); 
      }
     }
  }
}
public void lostOwnership(Clipboard clipboard, Transferable contents) {

相关文章