Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JFileChooser and FileFilter  RSS feed

 
Gulshan Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've looked at a lot of websites but I can't figure out how to make JFileChooser only show text files. Here's the code of the browse actionlistener:


How do I set up the filter?
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea what you mean by text files.

Have you read the JFileChooser API and followed the link to the Swing tutorial on "How to Use File Choosers"? There you will find an example that filters images. You can modify the example to meet your "text file" definition.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to the resources Rob mentioned, you can also take a look at the Adding a Filter to a File Chooser Dialog example at exampledepot.com
 
Gulshan Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:I have no idea what you mean by text files.

Have you read the JFileChooser API and followed the link to the Swing tutorial on "How to Use File Choosers"? There you will find an example that filters images. You can modify the example to meet your "text file" definition.


Oh, sorry. I meant files on windows computers with the .txt extension.

@Mark Vedder. Thanks for that link, I actually got it to only display text files from that code. Unfortunately I still have a problem. I still want the directories to show up so that the user can find the text file in a directory, and I tried using fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES) but that still didn't show the directories... any ideas?
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yet again, someone takes the easy way out and doesn't read the tutorial.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The tutorial Rob is referring to does indeed answer your second question. In fact, it gives a beautiful piece of sample code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!