I'm having problems with JFileChooser.
Seems that if you click on a directory name, that name will stick
to the "File Name:" box until you click on something else. This
causes a problem because if you click on a directory name, then
double click on it (thus going into that directory), the directory
name will still be in the "File Name:" box. So if the user
clicks "Open" at that point, they'll get a path that looks like
/blah/directory_name/directory_name
instead of just
/blah/directory_name
This is because the JFileChooser will combine the current
directory with the text on the "File Name:" line every
time, whether the final result exists or not.
So what can I do about that???
I came up with a completely nasty and evil kludge that clears
the "File Name:" box every time the user changes directories,
but I was wondering if there was a better way.
Thanx...
Thomas Weidenfeller - 15 Jun 2004 07:42 GMT
> So what can I do about that???
Not much. Directory selection was broken right from the beginning, and
this hasn't changed. You can search Sun's bug parade and vote for any
related bug - if you find them in the mess, and if they are not closed.
Or you can write an own bug report to Sun.
Don't hold you breath. JFileChooser was and is broken in many strange
ways. It is IMHO the second worst Swing component (JTable being the
worst). But the really sad thing is, not much is about to change in the
future, too. Sun just doesn't have its priorities right. E.g.instead of
spending time to fix the !@#$%^&* thing, Sun spent time when going from
1.3 to 1.4 to change the scrolling from vertical to horizontal. I mean,
come on, just because Windows until then had the most broken file
chooser in the world, thanks to its horizontal scrolling, Sun shouldn't
try to compete for that title. But they did, and they got the title :-(
> I came up with a completely nasty and evil kludge that clears
> the "File Name:" box every time the user changes directories,
> but I was wondering if there was a better way.
Since you didn't show us your hack, how can we judge? But it can't be
worse than the original code. May I guess? You walk down the
JFileChooser's component hierarchy, until you found the components you
need to mess with (using instanceof), you add event listeners and bang
the !@#$%^&* thing over the head when the event is fired?
/Thomas
Steve W. Jackson - 16 Jun 2004 20:46 GMT
>:I'm having problems with JFileChooser.
>:
[quoted text clipped - 22 lines]
>:
>:Thanx...
I guess you'll just have to show me some code that demonstrates this.
I've got at least two places in my app where a JFileChooser is used to
select a directory, and it does not exhibit this behavior. Nor do I
have any special workaround implemented.
I preset my chooser's starting directory. I set the dialog title. I
change its "Open" button to read "Select". And then I call its
showOpenDialog method. That's it. When I click on a directory, its
name ends up there as it should. When I double-click it, I navigate
into it as I should. When I click the "Select" button, the named item
is chosen and returned to my application. Simple -- and correct
behavior. And it's working on Linux (a few flavors I'm aware of) as
well as multiple Windows versions we're currently supporting.
BTW, all this is in Sun's 1.4.2.
= Steve =

Signature
Steve W. Jackson
Montgomery, Alabama
Andrew Thompson - 17 Jun 2004 12:51 GMT
> I guess you'll just have to show me some code that demonstrates this.
AKA an SSCCE
<http://www.physci.org/codes/sscce.jsp> ;-)

Signature
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
ak - 17 Jun 2004 13:56 GMT
> Seems that if you click on a directory name, that name will stick
> to the "File Name:" box until you click on something else. This
[quoted text clipped - 14 lines]
>
> So what can I do about that???
this problem does not appears with 1.4.2.
for earlier versions use following workaround:
chooser.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
String name = e.getPropertyName();
if(name.equalsIgnoreCase("directoryChanged")) {
chooser.setSelectedFile(new File(""));
}
}
});

Signature
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader