-
Bug
-
Resolution: Duplicate
-
P2
-
None
-
1.3.0
-
generic
-
windows_nt
When we drag a Japanese named file from Windows Explorer onto our app, the characters in the filename are corrupted so that the app cannot open/access the file. (The problem appears to happen with any non-ascii character, i.e. >127.)
The problem occurs with the JDK 1.3 on Windows 98, NT 4.0, and Windows 2000.
The following code demonstrates the problem. Try to drag a file whose name contains multi-byte or non-ascii characters onto the JFrame window that the app opens. The window will display an error message and the garbage characters that were received in the drop.
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class DropLabel extends JLabel implements DropTargetListener
{
public DropLabel(String s)
{
setText(s);
new DropTarget (this, DnDConstants.ACTION_COPY, this, true);
showDrop(false);
}
private void showDrop(boolean b)
{
setForeground (b?Color.white: Color.black);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setTitle("DropLabel test");
frame.getContentPane().add(new DropLabel("Drop here"));
frame.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300,100);
frame.setVisible(true);
}
/**
* Configure to desired flavor of dropped data.
*/
private DataFlavor getDesiredFlavor() {
return DataFlavor.javaFileListFlavor;
}
/**
* Check to make sure that the contains the expected object types.
*/
private void checkDroppedData(Object data)
{
System.out.println( "Got data: "+data.getClass().getName());
if (data instanceof AbstractList)
{
AbstractList files = (AbstractList)data;
if (((File)files.get(0)).isFile())
setText (((File)files.get(0)).toString());
else
setText("Error: not valid file: " +
((File)files.get(0)).toString());
}
else
{
System.out.println( "Error: wrong type of data dropped");
}
}
private boolean isDragOk(DropTargetDragEvent e)
{
boolean canDrop = false;
try
{
canDrop = e.isDataFlavorSupported(getDesiredFlavor());
}
catch(Exception ex) {}
if (canDrop)
e.acceptDrag(DnDConstants.ACTION_COPY);
else
e.rejectDrag();
showDrop (canDrop);
return canDrop;
}
public void dragEnter(DropTargetDragEvent e)
{
isDragOk(e);
}
public void dragOver(DropTargetDragEvent e)
{
isDragOk(e);
}
public void dropActionChanged(DropTargetDragEvent e)
{
isDragOk(e);
}
public void dragExit(DropTargetEvent e)
{
showDrop (false);
}
public void drop(DropTargetDropEvent e)
{
try
{
e.acceptDrop(DnDConstants.ACTION_COPY);
checkDroppedData(e.getTransferable().getTransferData(getDesiredFlavor()));
}
catch (Exception err) {}
e.dropComplete(true);
showDrop (false);
}
}
The problem occurs with the JDK 1.3 on Windows 98, NT 4.0, and Windows 2000.
The following code demonstrates the problem. Try to drag a file whose name contains multi-byte or non-ascii characters onto the JFrame window that the app opens. The window will display an error message and the garbage characters that were received in the drop.
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class DropLabel extends JLabel implements DropTargetListener
{
public DropLabel(String s)
{
setText(s);
new DropTarget (this, DnDConstants.ACTION_COPY, this, true);
showDrop(false);
}
private void showDrop(boolean b)
{
setForeground (b?Color.white: Color.black);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setTitle("DropLabel test");
frame.getContentPane().add(new DropLabel("Drop here"));
frame.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(300,100);
frame.setVisible(true);
}
/**
* Configure to desired flavor of dropped data.
*/
private DataFlavor getDesiredFlavor() {
return DataFlavor.javaFileListFlavor;
}
/**
* Check to make sure that the contains the expected object types.
*/
private void checkDroppedData(Object data)
{
System.out.println( "Got data: "+data.getClass().getName());
if (data instanceof AbstractList)
{
AbstractList files = (AbstractList)data;
if (((File)files.get(0)).isFile())
setText (((File)files.get(0)).toString());
else
setText("Error: not valid file: " +
((File)files.get(0)).toString());
}
else
{
System.out.println( "Error: wrong type of data dropped");
}
}
private boolean isDragOk(DropTargetDragEvent e)
{
boolean canDrop = false;
try
{
canDrop = e.isDataFlavorSupported(getDesiredFlavor());
}
catch(Exception ex) {}
if (canDrop)
e.acceptDrag(DnDConstants.ACTION_COPY);
else
e.rejectDrag();
showDrop (canDrop);
return canDrop;
}
public void dragEnter(DropTargetDragEvent e)
{
isDragOk(e);
}
public void dragOver(DropTargetDragEvent e)
{
isDragOk(e);
}
public void dropActionChanged(DropTargetDragEvent e)
{
isDragOk(e);
}
public void dragExit(DropTargetEvent e)
{
showDrop (false);
}
public void drop(DropTargetDropEvent e)
{
try
{
e.acceptDrop(DnDConstants.ACTION_COPY);
checkDroppedData(e.getTransferable().getTransferData(getDesiredFlavor()));
}
catch (Exception err) {}
e.dropComplete(true);
showDrop (false);
}
}
- duplicates
-
JDK-4187490 Win32: Japanese FileNames appear as ????? when dragging and dropping
-
- Resolved
-