-
Problem with SaveDialog & File extensions
I'm using JFileChooser class and I'm invoking the showSaveDialog method with a file filter on .txt files.
Here is the problem:
If I call the file in the dialog with the name of "test", I wish it creates a "test.txt" file (I've set the FileFilter for this reason), but it creates a file without extension called "test".
What is the purpuose of the FileFilter in the SaveDialogs, then?
Thanks
R.B.
-
From Sun's tutorial:
"By default, a file chooser displays all of the files and directories that it detects, except hidden files. A program can apply one or more file filters to a file chooser so that the chooser shows only some files. "
http://java.sun.com/docs/books/tutor...lechooser.html
ArchAngel.
O:-)
-
SaveDialog problems
I understand the usefullness of FileFilter in OpenDialog. But I think there are some problems when I try to use the FileFilter in SaveDialog. I wish the user hasn't to write the extension of the file he want to create. I wish the extension is fixed in someway, so I thought to use the FileFilter, but it seems not to work.
R.B.
-
I think you'll have to write that bit yourself. Fortunately, it's not that difficult.
ArchAngel.
O:-)
-
-
here's a tip:
from that part where you choose your file's savename, just put a .txt as an extension, everytime.
so savename + .txt = you got it! of course, this is assuming that you only wish to save in .txt file extensions.
now in order to make it more dynamic, if you're gonna be using a lot of filters, your app should know which filter was chosen. then from there, just add the appropriate extension. it has to "listen" to the filter selection. i don't know if java has a method for this by default, but it's certainly doable if it doesn't.
as archangel put it, "...it's not that difficult."
-
"so savename + .txt = you got it! of course, this is assuming that you only wish to save in .txt file extensions. "
Except if someone enters the filename "Hello.txt" as it will become "Hello.txt.txt".
ArchAngel.
O:-)
-
MyJFileChooser class
To solve my little problem I've created a MyJFileChooser class which extends the JFileChooser class. I've added the solution of my problem: now the FileFileter associate to the JFileChooser fixs the extensions of files created with a SaveDialog. Well, these are my intensions, but the truth is bit different.
The problem is this:
When the construstor of MyJFileChooser class is invoked there is a Null Pointer exeption caused (I suppose) by the fact that JFileChooser in his constructor makes use of some protected methods (e.g setup(FileSystemView view)) that can't be extended.
So how can I do If I wanted to add a particular new feature to a preexistent class like JFileChooser?
R.B.
-
Please post some code and your error message.
Also, you should be calling getFileFilter() which will return the currently selected filter.
ArchAngel.
O:-)
-
of course that's what a win Notepad would do too if the filter is set to .txt. so it doesn't matter, text editors will still read the file, and your OS can still associate it with a program to open it with. but then that's dumb.
there you go, getFileFilter() should do it!
-
Here is my code
Here is the code:
Code:
import java.io.File;
import javax.swing.*;
public class MyJFileChooser extends JFileChooser
{
public MyJFileChooser()
{
super();
}
public File getSelectedFile() {
File file = super.getSelectedFile();
// Controllo che non sia già stata messa a mano l'estensione
// se l'estensione corrisponde a quella selezionata con il filtro
// non aggiungo nulla, altrimenti aggiungo l'estensione
// I check user hasn't just specified the right exetension for the file.
// If It not so, I add it at the end of the pathname
String pathname = file.getAbsolutePath();
String extension = this.getFileFilter().getDescription();
if(!(pathname.endsWith(extension)))
pathname = pathname + extension;
File fileOut = new File(pathname);
return fileOut;
}
}
When I try to use the constructor:
Code:
MyJFileChooser myFiler = new MyJFileChooser();
I obtain a java.lang.NullPointerException
[ArchAngel added CODE tags]
R.B.
-
Solution to my SaveDialog problems
I've found the solution to my problem. The null-pointer exception was in my new overriden method getSelectedFile(), because I've not considered that the file read with super().getSelectedFile could be null.
Here is how I solved the problem in the code:
Code:
.
.
.
public File getSelectedFile() {
File file = super.getSelectedFile();
// Controllo che non sia già stata messa a mano l'estensione
// se l'estensione corrisponde a quella selezionata con il filtro
// non aggiungo nulla, altrimenti aggiungo l'estensione
// I check user hasn't just specified the right exetension for the file.
// If It not so, I add it at the end of the pathname
if(file != null)
{
String pathname = file.getAbsolutePath();
String extension = this.getFileFilter().getDescription();
if(!(pathname.endsWith(extension)))
pathname = pathname + extension;
File fileOut = new File(pathname);
return fileOut;
}
else
return null;
}
.
.
.
And that's all.
[ArchAngel added CODE tags]
R.B.
-
Thanks for posting your solution.
ArchAngel.
O:-)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks