Home arrow static arrow Java Programming [Archive] - JFileChooser selected file
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - JFileChooser selected file
This topic has 15 replies on 2 pages.    1 | 2 | Next »

Posts:41
Registered: 2/27/02
JFileChooser selected file  
Mar 29, 2002 8:38 AM



 
from my code below, the File Name in the dialog/file selection window defaults to the file but once I click on to a directory to change directory the File Name changed to the directory name. How do I set the filechooser to not change the File Name if a directory is clicked? Thanks, jude

...
File fileSelected = new File("c:\\temp
newfile.txt");
JFileChooser filechooser = new JFileChooser();
filechooser.setDialogTitle("Save to a text file.");
filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
filechooser.setSelectedFile(fileSelected);
int iStatus = filechooser.showOpenDialog(null);

if (iStatus == JFileChooser.APPROVE_OPTION) {
fileSelected = filechooser.getSelectedFile();
}
else if (iStatus == JFileChooser.CANCEL_OPTION) {
return;
}
...
 

Posts:8
Registered: 3/10/02
Re: JFileChooser selected file  
Mar 29, 2002 8:52 AM (reply 1 of 15)



 
u can try someting like fileSelected.isDirectory <-- this is a boolean
some short hints
 

Posts:41
Registered: 2/27/02
Re: JFileChooser selected file  
Mar 29, 2002 8:57 AM (reply 2 of 15)



 
thanks for the quick reply, I think JFileChooser.FILES_ONLY already handles this which only returns a file. What I need is that when already in the dialog window selecting for file, if I click on a directory, it should not change the "File Name" in the dialog since I have not clicked on a file. It should only change the "File Name" to a file name and not to a directory name.
 

Posts:13
Registered: 8/8/00
Re: JFileChooser selected file  
Apr 9, 2002 8:50 AM (reply 3 of 15)



 
hi jude,
Even i have not been able to get by this problem of the selected or typed in file name not staying there once a directory is selected. If you have found a round about way of doing this please let me know.
Thanks,
Regards,
Chet

from my code below, the File Name in the dialog/file
selection window defaults to the file but once I click
on to a directory to change directory the File Name
changed to the directory name. How do I set the
filechooser to not change the File Name if a directory
is clicked? Thanks, jude

...
File fileSelected = new
File("c:\\temp
newfile.txt");
JFileChooser filechooser = new JFileChooser();
filechooser.setDialogTitle("Save to a text file.");
filechooser.setFileSelectionMode(JFileChooser.FILES_ONL
);
filechooser.setSelectedFile(fileSelected);
int iStatus = filechooser.showOpenDialog(null);

if (iStatus == JFileChooser.APPROVE_OPTION) {
fileSelected = filechooser.getSelectedFile();
}
else if (iStatus == JFileChooser.CANCEL_OPTION) {
return;
}
...
 

Posts:4
Registered: 9/13/99
Re: JFileChooser selected file  
Apr 17, 2002 10:20 PM (reply 4 of 15)



 
I have noticed this problem as well and even posted a question to comp.lang.java.gui on April 11. No one responded so I looked for example code, such as the Notepad example that comes with 1.4.0 and is available through the Java Web Start application, and noticed that it has the same problem. So I believe this can be relegated to Sun to fix as it seems to be a bug in Java.
 

Posts:41
Registered: 2/27/02
Re: JFileChooser selected file  
Apr 18, 2002 5:59 AM (reply 5 of 15)



 
thanks, I will report too
 

Posts:47
Registered: 3/22/02
Re: JFileChooser selected file  
Apr 18, 2002 10:19 AM (reply 6 of 15)



 
You can't do that. The normal behavior for a file chooser is to put the name of the file selected in the file name box, if it could not do that it would be useless. As for not putting the name of a folder in the file box, it also does not make sense to prevent this from happening because the file chooser has to be able to open a folder even if only to show its contents. This is definitely not a bug but normal useful behavior of JFileChooser (in my opinion, of course).
 

Posts:4
Registered: 9/13/99
Re: JFileChooser selected file  
May 15, 2002 3:38 PM (reply 7 of 15)



 
Hmm, I believe you missed the point. The bug is that the file chooser blanks the file name text field when moving from one directory to another (this is not useful behavior for anyone else's file choosers, be it Microsoft, Apple, or what have you, so I don't know why it would be useful for Sun/Java). A Sun employee involved with Swing has informed me that this will be fixed as of release version 1.4.1.
 

Posts:945
Registered: 3/13/02
Re: JFileChooser selected file  
May 15, 2002 4:31 PM (reply 8 of 15)



 
In Windows ME, when I switch to another directory, the same file stays in the text box, but when I click open, an error box comes up.

Is that better?
 

Posts:1
Registered: 9/6/01
Re: JFileChooser selected file  
Jun 19, 2002 4:33 PM (reply 9 of 15)



 
I have been able to get the JFileChooser to behave in a more standard way when the user uses the mouse to select files. I have still not been able to find a way to get it to work the way I want if they've typed a file in manually. Does anyone know how to get to the JTextField in the JFileChooser to read the value?

If anyone could hook me up with how to access the JTextField I'd sure be greatful!!!

Here is how I 'fixed' the traversal of folders using the mouse.

I subclassed the JFileChooser and made a local variable
private File lastSelectedFile = null;

I then registered property changed listeners in the constructor for both file selection and directory changes:
this.addPropertyChangeListener(JFileChooser.DIRECTORY_CHANGED_PROPERTY,
(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
processDirChanged(e);
}
}));
this.addPropertyChangeListener(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY,
(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
processFileSelected(e);
}
}));

Here are the propertyChangedListeners:

public void processDirChanged(PropertyChangeEvent e) {
if ( lastSelectedFile != null ) {
File newSelectedFile = new File(this.getCurrentDirectory().getPath() + File.separator + this.lastSelectedFile.getName());
this.lastSelectedFile = newSelectedFile;
this.setSelectedFile(this.lastSelectedFile);
}
}

public void processFileSelected(PropertyChangeEvent e) {
File selectedFile = this.getSelectedFile();
if ( lastSelectedFile != null && ( selectedFile == null || selectedFile.isDirectory()) ) {
this.setSelectedFile(this.lastSelectedFile);
}
else {
lastSelectedFile = this.getSelectedFile();
}
}

 

Posts:6
Registered: 5/27/99
Re: JFileChooser selected file  
Jun 26, 2002 2:53 AM (reply 10 of 15)



 
You can get the value of the manually typed filename with

fc.getSelectedFile().getName();

where fc is the JFileChooser, of course

Hope this helps !
 

Posts:19
Registered: 11/3/02
Re: JFileChooser selected file  
Dec 13, 2002 4:26 AM (reply 11 of 15)



 
I met with the same problem!
I need help!
 

Posts:65
Registered: 5/7/02
Re: JFileChooser selected file  
Dec 13, 2002 12:22 PM (reply 12 of 15)



 
This is a known problem with the 1.4.0 and previous JRE's. This has been fixed in the 1.4.1 JRE.
 

Posts:1
Registered: 5/31/01
Re: JFileChooser selected file  
Sep 30, 2003 7:13 AM (reply 13 of 15)



 
Try working on something like that.

Subclass JFileChooser and override
setSelectedFile

public void setSelectedFile(File f)
{
if(f.isFile())
{
super.setSelectedFile(f);
}
}

That will solve the probleme of the file name
being overwritten.

Not to get the name of the file at any time,
especially on the approve action go like
that

FileChooserUI ui = getUI();
String fileName = "";
if (ui instanceof BasicFileChooserUI) //this should be true
{
fileName = ((BasicFileChooserUI)ui).getFileName();
}
 

Posts:446
Registered: 6/23/00
Re: JFileChooser selected file  
Sep 30, 2003 7:23 AM (reply 14 of 15)



 

Hmmm... :)
 
This topic has 15 replies on 2 pages.    1 | 2 | Next »