X

Geertjan's Blog

  • June 7, 2012

View Clipboard & Copy To Clipboard from NetBeans IDE

Geertjan Wielenga
Product Manager

Thanks to this code, I can press Ctrl-Alt-V in NetBeans IDE and then view whatever is in the clipboard:

import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JOptionPane;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.util.NbBundle.Messages;
@ActionID(
category = "Tools",
id = "org.demo.ShowClipboardAction")
@ActionRegistration(
displayName = "#CTL_ShowClipboardAction")
@ActionReferences({
@ActionReference(path = "Menu/Tools", position = 5),
@ActionReference(path = "Shortcuts", name = "DA-V")
})
@Messages("CTL_ShowClipboardAction=Show Clipboard")
public final class ShowClipboardAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, getClipboard(), "Clipboard Content", 1);
}
public String getClipboard() {
String text = null;
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
text = (String) t.getTransferData(DataFlavor.stringFlavor);
}
} catch (UnsupportedFlavorException e) {
} catch (IOException e) {
}
return text;
}
}

And now I can also press Ctrl-Alt-C, which copies the path of the current file to the clipboard:

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.awt.StatusDisplayer;
import org.openide.loaders.DataObject;
import org.openide.util.NbBundle.Messages;
@ActionID(
category = "Tools",
id = "org.demo.CopyPathToClipboard")
@ActionRegistration(
displayName = "#CTL_CopyPathToClipboard")
@ActionReferences({
@ActionReference(path = "Menu/Tools", position = 0),
@ActionReference(path = "Editors/Popup", position = 10),
@ActionReference(path = "Shortcuts", name = "DA-C")
})
@Messages("CTL_CopyPathToClipboard=Copy Path to Clipboard")
public final class CopyPathToClipboardAction implements ActionListener {
private final DataObject context;
public CopyPathToClipboardAction(DataObject context) {
this.context = context;
}
@Override
public void actionPerformed(ActionEvent e) {
String path = context.getPrimaryFile().getPath();
StatusDisplayer.getDefault().setStatusText(path);
StringSelection ss = new StringSelection(path);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(ss, null);
}
}

Join the discussion

Comments ( 1 )
  • guest Friday, January 27, 2017

    I want to copy columnHeaders of table along with contents. This seem to not working. Is there any way to copy columnHeaders of table in the clipboard.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha