Welcome to Dream.In.Code
Become a Java Expert!

Join 149,523 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,419 people online right now. Registration is fast and FREE... Join Now!




JFileChooser question

 
Reply to this topicStart new topic

JFileChooser question, allow opening sub directories

gyron
5 Jul, 2007 - 04:51 AM
Post #1

D.I.C Head
**

Joined: 9 Jan, 2007
Posts: 61


My Contributions
This is my first time to use a JFileChooser. What i'm trying to do is pop up a 'Save As' dialog. I have the following code and the dialog that comes up does not allow me to get into a folder to viw/open subfolders, if i may want to save into those sub folders.
Also; how do i make the program 'remember' the previous path so that the user doesn't navigate again when they want to save the file?

CODE

JFileChooser fc=new JFileChooser();
         fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
         int returnVal = fc.showOpenDialog(null);
         if (returnVal == JFileChooser.APPROVE_OPTION) {
            //get the file path and name for the new file
         } else {
            //saving cancelled
         }

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: JFileChooser Question
5 Jul, 2007 - 11:27 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
First of all you might want to use the method "showSaveDialog" if you want to make a save dialog open up. Right now you specify the "open" dialog which might be the reason you are having the trouble of saving using it. Second, to keep the directory where they last navigated to, simply keep a reference of the dialog. For instance, in your example if you were to use fc.showSaveDialog(null) again you would open up the same dialog and it would still be on the folder they last visited.

In large scale projects this may mean you need to bump up the filechooser variable "fc" to a higher scope so that you can then use it across various functions in your class. But that is just implementation details. Just know that you can reopen the dialog and not lose the directory you were last in by keeping a reference to your "fc" variable alive and well.

Other than that and maybe needing a filefilter you are ready to rock. Give it a try and let us know if you still have problems.

NOTE: You could also save the path from the dialog and when you go to relaunch the new dialog, set it to the saved path from last time it was used. (That is if you don't want to hold a reference to the dialog)

This post has been edited by Martyr2: 5 Jul, 2007 - 11:31 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:39PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month