• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JFileChooser and FileFilter

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic