Results 1 to 11 of 11
Thread: copy actual window to clipboard
- 04-01-2011, 09:41 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 22
- Rep Power
- 0
copy actual window to clipboard
I would like to create a screen shot of a set of diagrams. Currently I only can copy text, but no pictures :( . What I have in the moment (for test reasons with button):
Anyone has an idea, what I need to change to copy the current screen of the button to clipboard - ThanksJava Code:JButton copy = new JButton("Copy"); copy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String selection = jt.getSelectedText(); StringSelection data = new StringSelection(selection); clipboard.setContents(data, data); }
- 04-01-2011, 10:45 AM #2
java.awt.Robot
db
- 04-01-2011, 11:47 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 22
- Rep Power
- 0
Problem is, that I cannot enter the following code into the listener.
Any idea?Java Code:Robot robot = new Robot(); BufferedImage bufferedImage = robot.createScreenCapture(new Rectangle(new Dimension(200, 200)));
- 04-01-2011, 11:50 AM #4
Boo hoo, why not?I cannot enter the following code into the listener.
db
- 04-01-2011, 12:02 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 22
- Rep Power
- 0
The error message I get is: Unhandled exception type AWTException
Sorry, I could have provided this earlier.
- 04-01-2011, 12:07 PM #6
So handle the exception!
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
db
- 04-01-2011, 12:30 PM #7
Member
- Join Date
- Feb 2011
- Posts
- 22
- Rep Power
- 0
thanks for your advise, I have added the exception, but for some reason I still get the error. The code looks like:
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Robot robot = new Robot();
BufferedImage bufferedImage = robot.createScreenCapture(new Rectangle(new Dimension(200, 200)));
}
catch (AWTException e) {e.printStackTrace();}
}
});
The error message I get: Duplicate parameter e. May you have another idea, thanks for your patience.
- 04-01-2011, 12:37 PM #8
That error message is self explanatory. How many places in that code snippet is a parameter or variable e declared? Is is mandatory that such parameters/variables be called e?
db
- 04-01-2011, 01:39 PM #9
Member
- Join Date
- Feb 2011
- Posts
- 22
- Rep Power
- 0
sorry bother you again. All errors are removed and the program starts with no issues. The aim was to make a screen shot and I thought I can retreive it than with manual strg-v and place the copied image into a doc/ppt. But it is not doing it.
weekend is close and I will stop bothering you then ;-)
Code looks like this:
package demo;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.awt.Robot;
import javax.swing.*;
import java.awt.image.BufferedImage;
public class ClipLight {
public static void main(String args[]) {
JFrame frame = new JFrame("Clip");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
final Clipboard clipboard =
frame.getToolkit().getSystemClipboard();
final JTextArea jt = new JTextArea();
JScrollPane pane = new JScrollPane(jt);
frame.add(pane, BorderLayout.CENTER);
JButton copy = new JButton("Copy");
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent t) {
try {
Robot robot = new Robot();
BufferedImage bufferedImage = robot.createScreenCapture(new Rectangle(new Dimension(200, 200)));
}
catch (AWTException e) {e.printStackTrace();}
}
});
JPanel p = new JPanel();
p.add(copy);
frame.add(p, BorderLayout.SOUTH);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
- 04-01-2011, 02:01 PM #10
Programming isn't magic. You already know that a copy to the clipboard involves setting the clipboard's contents. Or was the code in your first post copied from somewhere and used blindly?I thought I can retreive it than with manual strg-v and place the copied image into a doc/ppt. But it is not doing it.
Study and understand, then adapt this code to your requirement.
Sending Image Objects through the Clipboard : Image*«*2D Graphics GUI*«*Java
db
- 04-01-2011, 02:18 PM #11
Member
- Join Date
- Feb 2011
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
Help!! Copy JTextField to clipboard!
By goffy in forum New To JavaReplies: 6Last Post: 05-01-2010, 07:37 PM -
Copy from Clipboard in japplet
By AZMichael in forum Java AppletsReplies: 3Last Post: 09-06-2008, 02:30 PM -
How to copy and paste data with the clipboard
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:35 PM -
Using the clipboard
By Java Tip in forum java.awt.datatransferReplies: 0Last Post: 04-16-2008, 10:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks