8 Replies Latest reply: Apr 20, 2008 11:51 AM by 807591 RSS

    Copy String to Clipboard

    807591
      Can this be done? In a simple way? It's not very important, but if i could do it it would be fairly helpful...
        • 1. Re: Copy String to Clipboard
          807591
          TheMungo wrote:
          Can this be done? In a simple way? It's not very important, but if i could do it it would be fairly helpful...
          See:
          java.awt.Toolkit.getSystemClipboard();
          • 2. Re: Copy String to Clipboard
            807591
            [http://www.javapractices.com/topic/TopicAction.do?Id=82|http://www.javapractices.com/topic/TopicAction.do?Id=82]

            that link shows you how to do it. It is based on the Clipboard class i think
            • 3. Re: Copy String to Clipboard
              DarrylBurke
              Would 2 lines be simple enough ;-)
              StringSelection ss = new StringSelection("Here you go!");
              Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, ss);
              Please take the trouble to look up the classes and methods in the API.

              db
              • 4. Re: Copy String to Clipboard
                807591
                I can't work this out at all...

                Say my code is:
                     try {
                     
                          BufferedReader read = new BufferedReader(new FileReader("SecretMessage.txt"));
                          JOptionPane.showMessageDialog(null,"Your Encode//Decoded Message is:\n\n" + read.readLine() + "\n\nMessage has also been copied to the clipboard.\nPress \"CTRL+V\" to paste this message.");
                          read.close();
                     }
                     catch (IOException e) {}
                And I want "read.readLine" to be copied with this automatically... Can somebody give me a hand?

                Edited by: TheMungo on Apr 17, 2008 6:00 AM
                • 5. Re: Copy String to Clipboard
                  807591
                  Pseudo code:
                  - Open your reader
                  - use read.readLine() to read a line of input and store this in a local String variable
                  - use the methods previously provided to store this string var in the clipboard
                  - show the dialog box with the variable embedded in the message
                  - close your reader
                  • 6. Re: Copy String to Clipboard
                    807591
                         try {
                         
                              BufferedReader read = new BufferedReader(new FileReader("SecretMessage.txt"));
                              String readMessage = read.readLine();
                              StringSelection ss = new StringSelection(readMessage);
                              Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, ss);
                              JOptionPane.showMessageDialog(null,"Your Encoded//Decoded Message is:\n\n" + readMessage + "\n\nMessage has also been copied to the clipboard.\nPress \"CTRL+V\" to paste this message.");
                              read.close();
                         }
                         catch (IOException e) {}
                    Gives me:

                    SecretMessage.java:173: cannot find symbol
                    symbol : class StringSelection
                    location: class SecretMessage
                              StringSelection ss = new StringSelection(readMessage);
                              ^
                    SecretMessage.java:173: cannot find symbol
                    symbol : class StringSelection
                    location: class SecretMessage
                              StringSelection ss = new StringSelection(readMessage);
                              ^
                    SecretMessage.java:174: cannot find symbol
                    symbol : variable Toolkit
                    location: class SecretMessage
                              Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, ss);
                              ^




                    Am I missing some packages or something?
                    • 7. Re: Copy String to Clipboard
                      DarrylBurke
                      You appear to have not imported the classes used.

                      db
                      • 8. Re: Copy String to Clipboard
                        807591
                        import java.awt.datatransfer.StringSelection;
                        import java.awt.*;
                        Ah, got it. Cheers!