我想在FileDialog中实现文件过滤功能,代码如下:   
    
    class Myfilter implements FilenameFilter//用于文件过滤
    {
        public boolean accept(File dir,String fname)
         {
             return fname.toLowerCase().endsWith(".txt");
         }
    }    public void jButton_actionPerformed(ActionEvent e)//单击按钮Button打开文件对话框
    {
         FileDialog fdlg=new FileDialog(this,"打开", FileDialog.LOAD);
         Myfilter filter=new Myfilter();
         fdlg.setFilenameFilter(filter);
         fdlg.setVisible(true);
    }但是却不能实现我想要的功能,打开文件对话框后,过滤条中的信息仍然是(*.*),哪位大哥能帮帮小妹呢?

解决方案 »

  1.   

    好像前两天也有一个兄弟用FileDialog解决不了文件过滤的问题,楼主可以搜一下看看结果,
    楼主也可以考虑用JFileChooser
      

  2.   

    用JFileChooser的过滤比较好一点。
      

  3.   

    用JFileChooser能行吗,~
    JFileChooser不是用来选择文件的吗
      

  4.   

    jFileChooser当然可以哈,但我想用FileDialog^-^
      

  5.   

    加上这句试试
    fdlg.setFileName("*.txt");
    不过只能过虑文件的类型不能具体到某个文件
      

  6.   

    小妹,看一下这个:
    public void setFilenameFilter(FilenameFilter filter)Sets the filename filter for this file dialog window to the specified filter. Filename filters do not function in Sun's reference implementation for Windows 95, 98, or NT 4.0. 
    你的机器是什么系统的?我是2003试了也不行,才看到这个。