how do I make jfilechooser only accept .txt

前端 未结 3 1430
慢半拍i
慢半拍i 2020-12-14 06:43

I trying to save my contact in my table but filechosser always setit to all file. is there way I can set it to accept .txt only and make it default or the only option.

相关标签:
3条回答
  • 2020-12-14 07:10

    You need to add a filter:

    JFileChooser jf = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
    jf.setFileFilter(filter);
    
    0 讨论(0)
  • 2020-12-14 07:21

    You could do that by using FileFilter.

    Create a Filefilter with the necessary conditions. Set this file filter to the JFileChooser, and launch it.

    0 讨论(0)
  • 2020-12-14 07:26

    Here some examples

    fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp"));
    fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("*.pdf", "pdf"));
    fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("*.txt", "txt"));
    
    0 讨论(0)
提交回复
热议问题