Copy/Paste to and from System Clipboard

Help
2005-10-25
2013-04-11
  • Is there any workaround to copy/paste data to and from the System clipboard?

     
    • Paul Zepernick
      Paul Zepernick
      2005-10-25

      The only way I found is to set the applet to detach in the conf file:

      Applet.detach            =    true

      This puts Copy Paste buttons on a toolbar.

      I am looking to try and add copy and paste functionality through the embeded applet.  Is anyone familiar enough with the code to give me a starting point?  I was looking at the ButtonBar class thinking maybe I could get it throught there.  I have very little applet experience and am pretty lost.  I think what I really need to know is how to grab the selected text on the applet from within the ButtonBar class.  I tried using the read() method, but I am not really sure what it does and I sent it into a perminent loop.

      TIA,

      Paul

       
    • Paul Zepernick
      Paul Zepernick
      2005-10-26

      I am still digging through the code trying to find a way to make the copy & paste work.  I am begining to understand a little bit about how the plugins are constructed. 

      Does anyone know how to get a reference to another plugin from inside a plugin?  Would I be able to reference the terminal plugin from inside the button bar plugin? If so, I think I can handle it from there.  

      Paul

       
      • the plugins dont really know about each other.

        they communicate via messages, (see de/mud/jta/event/*Listener.java and *Request.java)
        but programming those is not trivial.

         
    • I was hoping for a work around to copy text from another application(winword/notepad) to JTA.

       
    • Paul Zepernick
      Paul Zepernick
      2005-10-26

      OK I think I got it.  I ended up just changing jta.de.mud.plugin.Terminal

      I put the following code in the constructor.  I still need to do more testing, but it seems like it is working.  The copy and paste uses the windows system clipboard so you can copy and paste from a windows app -> JTA, JTA -> windows app, JTA -> JTA

      Anything highlighted in JTA will be put to the clipboard.  Right click will paste the current contents of the clipboard into JTA.

          //get a reference to the system clipboard. 
          try{
              //System.out.println(">>>>TRYING TO GET REFERENCE TO SYSTEM CLIPBOARD");
              clipboard = tPanel.getToolkit().getSystemClipboard();
              System.out.println("****GOT THE CLIPBOARD REFERENCE OK - COPY AND PASTE ENABLED****** :) :) :)");
          }catch(Exception ex){
              System.out.println("****FAILED TO GET CLIPBOARD - COPY AND PASTE WILL NOT WORK***** :( :( :( ");
              ex.printStackTrace();
          }
         
          //*******************************
          //code to handle copy and paste
          //from embeded terminal
          //*******************************
          terminal.addMouseListener(new MouseListener() {
              public void mouseClicked(MouseEvent me) {
                  //make sure it only does the paste on button2(right mouse)
                  if (me.getButton() == me.BUTTON3 && clipboard != null){
                   //   System.out.println("mouse button 2 clicked(right mouse)");
                      paste(clipboard);
                  }
              }
              public void mouseExited(MouseEvent arg0) {}
              public void mousePressed(MouseEvent arg0) {
                 // System.out.println(">>>>MOUSE pressed");
               }
              public void mouseReleased(MouseEvent me) {
                  //make sure it only does the copy on button 1 (left mouse)
                  //System.out.println(">>>>MOUSE RELEASED");
                  if (me.getButton() == me.BUTTON1 && clipboard != null){
                      String selection = terminal.getSelection();
                     // System.out.println(">>>>SELECTION = " + selection);
                      if (selection != null && selection.trim().length() > 0){
                          copy(clipboard);
                      }
                  }else{
                      //not left mouse
                     // System.out.println("NOT BUTTON 1(left mouse): " + me.getButton());
                  }
                 
              }
              public void mouseEntered(MouseEvent arg0) {}
          });

      I also added the following property to the class:

        /**access to the system clipboard*/
        private Clipboard clipboard = null;

      You can email me at paulz17 at yahoo.com if you have any questions.

      Paul

       
    • Paul Zepernick
      Paul Zepernick
      2005-10-26

      Sorry, I should have added that the code should be pasted right after:

      terminal.addFocusListener(new FocusListener() {

      ......

      {);

      Paul

       
    • Paul Zepernick
      Paul Zepernick
      2005-10-27

       
    • thanks a million, Paul.

      Marcus,

      Is it possible to incorporate this into the jta26.jar? I am of the opinion that others might be interested.

      thanks,
      Bee Hock(beehock@mail.com)

       
      • i have applied the patch.

        will be in the next version Matthias releases ;)

        Ciao, Marcus

         
    • Paul,

      How do I do the copy from JTA to another application? Left click doesnt do anything.

      Btw, I am using the applet without detaching.

      Got the following message when trying to right click in JTA:

      Exception in thread "AWT-EventQueue-7" java.lang.ArrayIndexOutOfBoundsException: -2
          at de.mud.terminal.SwingTerminal.mouseReleased(SwingTerminal.java:869)
          at de.mud.terminal.SwingTerminal.processMouseEvent(SwingTerminal.java:968)
          at java.awt.Component.processEvent(Unknown Source)
          at java.awt.Component.dispatchEventImpl(Unknown Source)
          at java.awt.Component.dispatchEvent(Unknown Source)
          at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
          at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
          at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
          at java.awt.Container.dispatchEventImpl(Unknown Source)
          at java.awt.Component.dispatchEvent(Unknown Source)
          at java.awt.EventQueue.dispatchEvent(Unknown Source)
          at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
          at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
          at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
          at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
          at java.awt.EventDispatchThread.run(Unknown Source)

       
      • Paul Zepernick
        Paul Zepernick
        2005-10-28

        Is the applet signed, even if not, an exception should not be thrown, it should just not work.  When the applet starts it should be printing a message to the console as to whether or not it gained access to the system console.  Do you see either message? 

        Did you try and copy and paste the changes in from this post, or did you download the Terminal.java file in the patch?  I would suggest downloading from the patch if it is the former.  Maybe there was an error when placing the code in the file.

        Paul

         
      • Paul Zepernick
        Paul Zepernick
        2005-10-28

        Alomost forgot.  To copy from JTA to another app:

        1.  Highlight the text you want copied in JTA, by using the left mouse button. 

        2.  Go into the windows app and select paste.

        To copy from windows app to JTA :

        1.  Select the text you want to copy from inside the windows app and select the copy option.

        2.  In JTA make sure your cursor is in the field you wish to copy to and right click with the mouse on the JTA screen.

        Paul

         
    • Marcus,

      Can you email me the update with the patch in the meantime? :)

      thanks,
      Bee Hock(beehock@mail.com)

       
    • Hi Marcus,

      Any news on the copy/paste patch?

      thanks,
      Bee Hock.

       
      • jta26.jar build for testing with your patch:

        http://www.lst.de/~mm/jta26.jar

        Ciao, Marcus

         
    • Thanks, Marcus.

      Btw, the patch was kindly contributed by Paul.

      Paul,

      The patch didn't work and I have this message "Failed to get clipboard - copy and paste will not work" error.

      Any idea why the exception is throw?

       
    • The jar has to be signed using the jarsigner tool that comes in the java sdk.  It is in the bin folder of the j2sdk installation. 

      Here is an example usage, you must have a key created to sign the jar file.

      1.  Create key, if you already have one you can just use an existing key when signing:

      C:\j2sdk1.4.2_07\bin\keytool -genkey -alias MyKeyName -keypass MyKeyPass

      2.  Sign the jar like so
      C:\j2sdk1.4.2_07\bin\jarsigner c:\jta26\jta26.jar MyKeyName

      You will be prompted for the password for the key.  After correctly entering the password, the jar will be signed :)

      You may need to alter the paths to jarsigner & keytool depending on where you have them installed, and what your OS is.

      Paul

       
    • thanks, Paul. It work like a charm under IE. However, Firefox 1.07 does not seem to react at all.

      No prompt of signed applet as IE did.

      regards,
      Bee Hock.

       
    • Thats odd.  It is working for me in both browsers.  Maybe try re-installing the plugin in the browser.  If using windows go into control panel --> Java Plugin --> Browswer Tab.  Uncheck Mozilla, click apply, re check, click apply.

      Maybe try clearing the cache as well??

      Paul

       
    • Great advice, Paul.

      Firefox work after I clear the java cache.

      Hopefully, Marcus can help to sign the applet before he made the official release. JTA is an excellent product.

      thanks,
      Bee Hock.