Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

JFileChooser and FileFilter

 
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?
 
Rancher
Posts: 3208
29
  • 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.
 
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
Rancher
Posts: 3208
29
  • 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.
 
That new kid is a freak. Show him this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic