FileDailog, Urgent
Hello
I'm using FileDialog(AWT component) to open a file.
How to show files of type in dropdown list.(*.txt,*.dst.*.*).
i'm using AWT components.
please, possible give an example
thanx
sat
[223 byte] By [
sat] at [2007-11-15 19:14:22]

# 1 Re: FileDailog, Urgent
For this u will have to create a 'FilenameFilter' of ur own. Create a class implementing this Interface.
Then create a 'accept' method in that class such that, it returns 'true' for all the files you want in the list.
So if u want '*.txt' i.e. all files having 'txt' extension, u can write...
public boolean accept( File dir, String name )
{
return name.endsWith( ".txt" );
}
That's it. And then if u do 'setFilenameFilter', u will get the desired result.
- UnicMan
http://members.tripod.com/unicman
# 2 Re: FileDailog, Urgent
Hi unicman
Thanx for ur suggetion regarding fileDialog. As suggested by u, i created my own FilenameFilter class, and set to filedialog.
i have written follwing code, and i tested. but i'm not getting *.Txt in "Files of Type " combobox in fileDialog. in that combobox, getting only AllFiles(*.*) only.
import java.io.*;
import java.awt.*;
public class UseFileDialog {
public String loadFile(Frame f, String title) {
FileDialog fd = new FileDialog(f, title, FileDialog.LOAD);
//setting the filter
fd.setFilenameFilter(new MyFilter());
fd.setLocation(50, 50);
fd.show();
return fd.getFile();
}
public static void main(String s[]) {
UseFileDialog ufd = new UseFileDialog();
ufd.loadFile(new Frame(), "Open...");
System.exit(0);
}
}
// my filter class
class MyFilter implements FilenameFilter
{
public boolean accept( File dir, String name )
{
return name.endsWith( ".txt" );
}
}
how to disply FilesofType Combobox AllFiles(*.*) and *.txt etc..
if select *.txt in combobox , only should display those type of files.
thanx.....
sat
sat at 2007-11-10 3:02:29 >

# 3 Re: FileDailog, Urgent
Hi,
Use the method setFile(String) in FileDialog class.
fileDialog.setFile("*.txt");
This will work fine.
regards,
arun...
# 4 Re: FileDailog, Urgent
Hi Arun...
ThanX for Ur suggetion, but actual my problem is:
in fileDialog, there is a combobox with label Files of Type. in that combobox(drop down list) i have to display list of extensions(*.*, *.txt, *.csv etc..).
How to add these extensions in dropdownlist?
thanx..
sat
sat at 2007-11-10 3:04:30 >

# 5 Re: FileDailog, Urgent
Hi,
I think u can't do this in this FileDialog class as we don't have the control on this. Because they are using the native method for poping up the Dialog box.
But you can do this using JFileChooser class in javax.swing.*. For including the "*.txt", "*.html" in the combobox,u have to use some classses in javax.swing.filechooser.* . More specifically FileFilter class. And then in JFileChooser Class u have to call addChoosableFileFilter(FileFilter filter). This method adds a filter to the list of user choosable file filters. This whole code of JFileChooser is written in java. So we can have our hand on that.
Hope this will help. And sorry I have misunderstood your requirement in the last one.
regards,
arun...
If u have any problem mail me.
# 6 Re: FileDailog, Urgent
Hi Arun,
Thanx for ur suggestion and clear description about the problem.
I'm working on com.sun.java.swing.*.
if possible, please give me ur mail id, i'll contact u, to get valuable suggestions from u.
ThanQ
regards
sat
sat at 2007-11-10 3:06:37 >
