JDK-4213197 : Clipboard failure for substring
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.2.0,1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_95,windows_98
  • CPU: x86
  • Submitted: 1999-02-19
  • Updated: 2001-01-09
  • Resolved: 2001-01-09
Related Reports
Duplicate :  
Description

Name: dbT83986			Date: 02/19/99


The following code snippet should put the string
'xyz' into the clipboard.  After running this program, pasting the clipboard's contents to a text editor, such as Microsoft NOTEPAD, pastes 'abc' instead of 'xyz'.  Retrieving the string inside the same program from the Clipboard correctly gets 'xyz', though.
This problem is caused by putting strings generated by a 'substring' function into the clipboard, the workaround, shown in the snippet, is to create a new string that doesn't involve a .substring call.
This problem did not exist in 1.1

public class bug
{
public static void main(String[] args)
{
   Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
   String ms = "abcxyz";
   String text = ms.substring(3,6);
   // Uncomment next line for workaround:
   // text = new String(text);
   StringSelection contents = new StringSelection(text);
   clipboard.setContents(contents, contents);
}
}

(Review ID: 47589)
======================================================================

Name: yyT116575			Date: 01/09/2001


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


import java.awt.*;
import java.awt.datatransfer.*;

/**  the following code should paste "is a test." to the clipboard.
     instead, it pastes "this is a"
*/
public class SelectionBug implements ClipboardOwner {
  public static void main(String[] args) {
    new SelectionBug();
  }
  public SelectionBug() {
    Frame f=new Frame();
    Clipboard clipboard=f.getToolkit().getSystemClipboard();
    String str="this is a test.";
    StringSelection contents=new StringSelection(str.substring(5));
    clipboard.setContents(contents, SelectionBug.this);
  }
  public void lostOwnership(Clipboard c, Transferable t) {}
}
(Review ID: 114618)
======================================================================

Comments
WORK AROUND Name: dbT83986 Date: 02/19/99 Don't use strings generating by .substring() in calls to Clipboard.setContents(). ======================================================================
11-06-2004

EVALUATION This was fixed in ladybird and merlin during the data transfer rewrite. david.mendenhall@east 2001-01-09
09-01-2001