This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have a application that has multiple writers, meaning I can save the data in several formats. So I give the user the option to save a file. Many applications have the drop down of the file type you'd like to save. For example, Photoshop may allow you to save the picture as a .jpg, .png, .gif, etc. Based on the selection, the appropriate writer can be used. How can I do this in Java? Thanks!
Use a JFileChooser. You can add javax.swing.filechooser.FileFilter implementations using addChoosableFileFilter, and retrieve the currently selected one with getFileFilter(). So your code should look a bit like this:
Note that by default, there is a "select all" file filter. You can remove this using fileChooser.setAcceptAllFileFilterUsed(false).