Results 1 to 11 of 11
  1. #1
    greenhorn is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default 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):
    Java 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);
    }
    Anyone has an idea, what I need to change to copy the current screen of the button to clipboard - Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default

    java.awt.Robot

    db

  3. #3
    greenhorn is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    Problem is, that I cannot enter the following code into the listener.
    Java Code:
    Robot robot = new Robot(); 
    BufferedImage bufferedImage = robot.createScreenCapture(new Rectangle(new Dimension(200, 200)));
    Any idea?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default

    I cannot enter the following code into the listener.
    Boo hoo, why not?

    db

  5. #5
    greenhorn is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    The error message I get is: Unhandled exception type AWTException
    Sorry, I could have provided this earlier.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

  7. #7
    greenhorn is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

    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.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default

    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

  9. #9
    greenhorn is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default

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

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default

    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.
    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?

    Study and understand, then adapt this code to your requirement.
    Sending Image Objects through the Clipboard : Image*«*2D Graphics GUI*«*Java

    db

  11. #11
    greenhorn is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

Similar Threads

  1. Help!! Copy JTextField to clipboard!
    By goffy in forum New To Java
    Replies: 6
    Last Post: 05-01-2010, 07:37 PM
  2. Copy from Clipboard in japplet
    By AZMichael in forum Java Applets
    Replies: 3
    Last Post: 09-06-2008, 02:30 PM
  3. How to copy and paste data with the clipboard
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:35 PM
  4. Using the clipboard
    By Java Tip in forum java.awt.datatransfer
    Replies: 0
    Last Post: 04-16-2008, 10:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •