adjust selected File to FileFilter in a JFileChooser

前端 未结 8 729
死守一世寂寞
死守一世寂寞 2020-12-11 02:22

I\'m writing a diagram editor in java. This app has the option to export to various standard image formats such as .jpg, .png etc. When the user clicks File->Export, you get

8条回答
  •  执笔经年
    2020-12-11 02:47

    How about this:

    class MyFileChooser extends JFileChooser {
       public void setFileFilter(FileFilter filter) {
    
        super.setFileFilter(filter);
    
        FileChooserUI ui = getUI();
    
        if( ui instanceof BasicFileChooserUI ) {
         BasicFileChooserUI bui = (BasicFileChooserUI) ui;
    
         String file = bui.getFileName();
    
         if( file != null ) {
          String newFileName = ... change extension 
          bui.setFileName( newFileName );
         }
        }
       }
      }
    

提交回复
热议问题