Clipboard Services
getBrowserClipboard (1)
public BrowserTransferable getBrowserClipboard();
Description:
To get the clipboard data received from browser after CTRL+V key events.
Browser security allows access to clipboard only in these events.
Typically used for customized clipboard integration, while the built-in integration is disabled in configuration ("allowLocalClipboard" is false).
You can use the following steps:
1. Create a new context menu item called "Paste from browser".
2. When the user clicks on the context menu item, open a modal dialog asking the user to press CTRL+V.
3. Listen for the CTRL+V keystroke in the modal dialog.
4. When the user presses CTRL+V, call the following method to get the clipboard content.
5. Once you have the clipboard content, you can display it in the modal dialog or use it however you need.
Returns:
Latest clipboard content received from browser
Example:
private void getBrowserClipboardExample1() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
BrowserTransferable transferable = webswingApi.getBrowserClipboard();
if (transferable != null) {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferable, null);
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
if (cb.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
try {
String copyText = (String) cb.getData(DataFlavor.stringFlavor);
console.append("You pasted text:\n");
console.append(copyText + "\n");
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
}
}
}
getBrowserClipboard (2)
public BrowserTransferable getBrowserClipboard(PasteRequestContext ctx);
Description:
Request the user to paste from browser clipboard by showing a built-in html modal dialog.
This method will block EDT thread (by showing an invisible modal JDialog) until response received from user.
Typically used for customized clipboard integration, while the built-in integration is disabled in configuration ("allowLocalClipboard" is false).
Parameters:
ctx request context
Returns:
User submited clipboard content received from browser (null if cancelled)
Example:
private void getBrowserClipboardExample2() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
PasteRequestContext ctx = new PasteRequestContext();
ctx.setTitle("Please paste content");
ctx.setMessage("Use ctrl+v or paste from context menu to input field below.");
BrowserTransferable transferable = webswingApi.getBrowserClipboard(ctx);
if (transferable != null) {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferable, null);
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
if (cb.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
try {
String copyText = (String) cb.getData(DataFlavor.stringFlavor);
console.append("You pasted text:\n");
console.append(copyText + "\n");
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
}
}
}
sendClipboard (1)
public void sendClipboard(WebswingClipboardData content);
Description:
Sends the specified data to browser.
A toolbar will appear in browser displaying the data.
User can click or press CTRL+C to store the content to local clipboard.
Typically used for customized clipboard integration, while the built-in integration is disabled in configuration ("allowLocalClipboard" is false).
Parameters:
content clipboard data to be sent to browser
Example:
private void sendClipboardExample1() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
JLabel clipboardLabel = new JLabel();
JPanel sendPanel = new JPanel() {{
setPreferredSize(new Dimension(500, 500));
setBorder(BorderFactory.createTitledBorder("The specified clipboard content:"));
add(new JScrollPane(new JPanel() {{
add(clipboardLabel);
}}));
}};
JPanel clipboardPanel = new JPanel() {{
setPreferredSize(new Dimension(400, 400));
setBorder(BorderFactory.createTitledBorder("Clipboard Panel"));
JRadioButton plainTextRadioButton = new JRadioButton("PLAIN TEXT") {{
setPreferredSize(new Dimension(250, 35));
setBorder(new EmptyBorder(10, 10, 10, 10));
setSelected(true);
}};
JRadioButton imageRadioButton = new JRadioButton("IMAGE") {{
setPreferredSize(new Dimension(250, 35));
setBorder(new EmptyBorder(10, 10, 10, 10));
}};
JRadioButton htmlRadioButton = new JRadioButton("HTML TEXT") {{
setPreferredSize(new Dimension(250, 35));
setBorder(new EmptyBorder(10, 10, 10, 10));
}};
// JRadioButton fileRadioButton = new JRadioButton("FILE") {{
// setPreferredSize(new Dimension(250, 35));
// setBorder(new EmptyBorder(10, 10, 10, 10));
// }};
ButtonGroup bg = new ButtonGroup();
bg.add(plainTextRadioButton);
bg.add(imageRadioButton);
bg.add(htmlRadioButton);
// bg.add(fileRadioButton);
add(new JLabel() {{
setBorder(new EmptyBorder(10, 10, 10, 10));
setText("Select what do you want to send:");
}});
add(Box.createRigidArea(new Dimension(10, 10)));
add(plainTextRadioButton);
add(imageRadioButton);
add(htmlRadioButton);
// add(fileRadioButton);
add(Box.createRigidArea(new Dimension(10, 10)));
add(new JButton("Send specified clipboard to browser") {{
setPreferredSize(new Dimension(250, 35));
setMinimumSize(new Dimension(250, 35));
setMaximumSize(new Dimension(250, 35));
setBorder(new EmptyBorder(10, 10, 10, 10));
addActionListener(e -> {
try {
StringBuilder sb;
clipboardLabel.setText(null);
clipboardLabel.setIcon(null);
// create the specified data
WebswingClipboardData data = new WebswingClipboardData();
if (plainTextRadioButton.isSelected()) {
String plainText = "Hello Webswing";
data.setText(plainText);
clipboardLabel.setText(plainText);
sb = new StringBuilder();
sb.append("Your text that you sent to browser is:\n");
sb.append(plainText);
sb.append("\nPlease, paste this content to some text editor application [Microsoft Word]\n");
} else if (imageRadioButton.isSelected()) {
sb = new StringBuilder();
URL imageUrl = DragPictureDemo.class.getResource("resources/images/4.jpg");
if (imageUrl == null) {
sb.append("Image does not exist\n");
JOptionPane.showMessageDialog(null, sb.toString());
return;
} else {
BufferedImage bImage = ImageIO.read(imageUrl);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", bos);
byte[] dataImage = bos.toByteArray();
data.setImg(dataImage);
clipboardLabel.setIcon(new ImageIcon(dataImage));
sb.append("You sent image to browser\nPlease, paste this content to some image application [Microsoft Word]\n");
}
} else if (htmlRadioButton.isSelected()) {
String htmlText = "<p> Formatted <b>HTML</b> text</p>";
data.setHtml(htmlText);
clipboardLabel.setText(htmlText);
sb = new StringBuilder();
sb.append("Your html text that you sent to browser is:\n");
sb.append(htmlText);
sb.append("\nPlease, paste this content to some text editor application [Microsoft Word]\n");
// } else if (fileRadioButton.isSelected()) {
// ArrayList<String> files = new ArrayList<>();
// files.add("");
// data.setFiles(files);
// StringBuilder fileName = new StringBuilder();
// for (String file : files) {
// fileName.append(file).append("\n");
// }
// clipboardLabel.setText(fileName.toString());
//
// sb = new StringBuilder();
// sb.append("File that you sent to browser is:\n");
// sb.append(fileName);
// sb.append("\nPlease, download the file.\n");
} else {
console.append("Unsupported current clipboard content type. Please, in your swing application copy some string, image, file to clipboard.\n");
return;
}
int result = JOptionPane.showConfirmDialog(null, sendPanel, "Send specified clipboard to browser", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
webswingApi.sendClipboard(data);
JOptionPane.showMessageDialog(null, sb.toString());
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
console.append(ex.getMessage());
}
});
}});
}};
clipboardPanel.setLayout(new BoxLayout(clipboardPanel, BoxLayout.Y_AXIS));
JDialog dialog = new JDialog() {{
setTitle("Send programmatically clipboard to browser");
setSize(800, 800);
setLocationRelativeTo(null);
setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
}};
dialog.getContentPane().add(clipboardPanel);
dialog.setVisible(true);
}
}
sendClipboard (2)
public void sendClipboard();
Description:
Sends the current Swing clipboard content to browser.
A toolbar will appear in browser displaying the data.
User can click or press CTRL+C to store the content to local clipboard.
Typically used for customized clipboard integration, while the built-in integration is disabled in configuration ("allowLocalClipboard" is false).
Example:
private void sendClipboardExample2() {
WebswingApi webswingApi = WebswingUtil.getWebswingApi();
if (webswingApi != null) {
JLabel clipboardLabel = new JLabel();
JPanel sendPanel = new JPanel() {{
setPreferredSize(new Dimension(500, 500));
setBorder(BorderFactory.createTitledBorder("Current clipboard content:"));
add(new JScrollPane(new JPanel() {{
add(clipboardLabel);
}}));
}};
try {
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
StringBuilder sb;
clipboardLabel.setText(null);
clipboardLabel.setIcon(null);
if (cb.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
// PLAIN TEXT
String plainText = (String) cb.getData(DataFlavor.stringFlavor);
clipboardLabel.setText(plainText);
sb = new StringBuilder();
sb.append("Your text that you sent to browser is:\n");
sb.append(plainText);
sb.append("\nPlease, paste this content to some text editor application [Microsoft Word]\n");
} else if (cb.isDataFlavorAvailable(DataFlavor.imageFlavor)) {
// IMAGE
Image v = (Image) cb.getData(DataFlavor.imageFlavor);
clipboardLabel.setIcon(new ImageIcon(v));
cb.setContents(new Transferable() {
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.imageFlavor };
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return Arrays.asList(getTransferDataFlavors()).contains(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor) {
return v;
}
}, null);
sb = new StringBuilder();
sb.append("You sent image to browser\nPlease, paste this content to some image application [Microsoft Word]\n");
} else if (cb.isDataFlavorAvailable(new DataFlavor("text/html;class=java.lang.String"))) {
// HTML
String html = (String) cb.getData(new DataFlavor("text/html;class=java.lang.String"));
clipboardLabel.setText(html);
sb = new StringBuilder();
sb.append("Your html text that you sent to browser is:\n");
sb.append(html);
sb.append("\nPlease, paste this content to some text editor application [Microsoft Word]\n");
} else if (cb.isDataFlavorAvailable(DataFlavor.javaFileListFlavor)) {
// FILES
List<File> v = (List<File>) cb.getData(DataFlavor.javaFileListFlavor);
String fileName = v.get(0).getName();
clipboardLabel.setText(fileName);
sb = new StringBuilder();
sb.append("File that you sent to browser is:\n");
sb.append(fileName);
sb.append("\nPlease, download the file.\n");
} else {
// ERROR
console.append("Unsupported clipboard content type. Please, copy (CRTL+C) some string, image, file in your swing application.\n");
console.append("For example,\nin [Webswing Demo] application go to [Picture Dnd].\nClick to one image, use CTRL+C.\nThen go back to [Webswing API - sendClipboard (2) - Test method] \n");
return;
}
int result = JOptionPane.showConfirmDialog(null, sendPanel, "Send current clipboard to browser", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
webswingApi.sendClipboard();
console.append(sb.toString());
}
} catch (UnsupportedFlavorException | IOException | ClassNotFoundException ex) {
System.out.println(ex.getMessage());
console.append(ex.getMessage());
}
}
}