整整搞了2天,别人用的好好的JFileChooser jfc = new JFileChooser();到我这里就直接屏幕挂了,nnd,难道真是我的人品不行吗?
查了google才知道,这是jdk1.5的一个bug,可以用putClientProperty("FileChooser.useShellFolder", Boolean.FALSE);的方法来修正
或者你直接去jre包下rt.jar下 sun/awt/shell/Win32ShellFolder2.class文件直接替换成jdk1.4中的文件也可以.
原文在下面:
Bug ID:
6317789
Votes
2
Synopsis
REGRESSION: JFileChooser() hangs when encountering a shortcut to the desktop
Category
java:classes_swing
Reported Against
Release Fixed
mustang(b84)
State
10-Fix Delivered, bug
Priority:
3-Medium
Related Bugs
Submit Date
30-AUG-2005
Description
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)
ADDITIONAL OS VERSION INFORMATION :
Windows XP Service Pack 2 (also happens /w service pack 1)
EXTRA RELEVANT SYSTEM CONFIGURATION :
Not hardware specific
A DESCRIPTION OF THE PROBLEM :
Java hangs completely when JFileChooser() is called when a shortcut to the desktop is present on the desktop of Windows XP.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
In windows XP right-click on the desktop, select "new" from the popup menu then select "shortcut:". In the popup-dialog select "browse", then select the topmost item in the tree "desktop". Select ok. This will create a shortcut to the desktop on the desktop. I have used a shortcut like this for quick access to my desktop icons from a popup window (works well when assigned a hotkey).
Next, create a simple java program that simply calls the default constructor of JFileChooser - ie:
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFileChooser fileChooser = new JFileChooser();
};
});
}
}
When run with JDK 5.0 this program will hang. However, if you delete the desktop link the program runs to completion. If you run this program with JDK 1.4 it works fine - even with the desktop link.
As a final note, the bug is not present if you set the "useShellFolder" property to false, ie:
JFileChooser fileChooser = new JFileChooser() {
public void updateUI() {
putClientProperty("FileChooser.useShellFolder", Boolean.FALSE);
super.updateUI();
}
};
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I did not expect java to hang.
ACTUAL -
Java hung and the program had to be terminated by task manager.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// java will hang here if a particular shortcut is present on the desktop
JFileChooser fileChooser = new JFileChooser();
};
});
}
}
See steps above for creating the shortcut on the desktop.
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Delete the desktop link (see steps for recreating) or add
putClientProperty("FileChooser.useShellFolder", Boolean.FALSE);
to all JFileChooserClasses.
Release Regression From : 1.4.2
The above release value was the last known release where this bug was known to work. Since then there has been a regression.
Release Regression From : 1.4.2
The above release value was the last known release where this bug was known to work. Since then there has been a regression.
Posted Date : 2005-09-13 22:12:39.0
Work Around
N/A
Evaluation
Need to verify that bug is present in Mustang beta.
Posted Date : 2005-11-08 00:51:07.0
The cause is that native Win32ShellFolder2.copyFirstPIDLEntry() can return 0 if the ID list is empty, and this is the case for a shortcut to the Desktop created on the Desktop itself. This situation was not handled in Win32ShellFolder2.createShellFolderFromRelativePIDL().
It didn't appear earlier because JFileChooser didn't supported shortcuts. Since xxxxx@xxxxx added the support for shortcuts as the fix for 4356160, it became possible to do that trick with shortcut looping.
Posted Date : 2006-03-23 16:09:32.0
Comments
Include a link with my name & email
Submitted On 20-MAR-2006
The putClientProperty work around suggested above still does not allow the JFileChooser to open a folder with a shortcut link to the Desktop. It only prevents it from going into an infinite loop. The only real workaround is to not have any shortcut links to the Desktop in a folder.
Worse - this is something that was working (in 1.4.2) which is now broken.
Submitted On 07-JUL-2006
This problem also happens if you have a desktop short cut to windows explorer which is set to open up on the desktop directory (any other directory is not a problem). This should be fixed in Java 5 also, it totally freezes a program like Netbeans if you have the wrong kind of shortcut present.
Submitted On 24-JUL-2006
thomas.strauss
This bug is still not fixed in the build 1.5.0_07-b03
Submitted On 14-SEP-2006
Before
I had this problems until this morning, when I checked this bug, but the flaw came from a shortcut on my desktop that pointed to a remote folder, on a machine that was down (I'm working on Windows 2000 SP4).
I suppose that the JFC slowness resulted in too long try to resolve the shortcut (but, when my computer was disconnected from the network, there was no problem !). => try to reduce some time out ?
=> or resolve shortcuts only if the user try to use them ?
Please fix the JFC problems, it has a very high negative impact on our end user customers...
Thanks
Submitted On 13-OCT-2006
rapeko
I have similar problem with this FileChooser. I had very cpu consuming filechooser when I had 6 zip files on my desktop. I removed those files from desktop (Moved to another folder) and filechooser works perfectly withoout cpu utilization. I added some other zip-files to desktop and filechooser slows.... as many zips you have on your desktop the filechooser slows down...
And the cpu utilization was in java (not in virus scanning as you could think it would be...)
My system is:
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)
WinXP professional SP2
Submitted On 23-JAN-2008
Ofra
Is this bug going to be fixed in JAVA 5.0?
It's a very serious bug!
源文档 <http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6317789>