Using JFileChooser to select directory only

Hi. I am using JFileChooser to let the user to select the directory to save some files into the user-selected directory. I set the JFileChooser to show only directory and not files. Below show part of my code:
private JFileChooser chooser = new JFileChooser();
chooser.setFileHidingEnabled(true);
chooser.setMultiSelectionEnabled(false);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
     chooser.setDialogType(JFileChooser.OPEN_DIALOG);
int state = chooser.showDialog(frame, "Select Directory");
if (state == JFileChooser.APPROVE_OPTION )
file = chooser.getCurrentDirectory();
However, the File reference that i get is not the user-selected directory. Please help.
Thank you.

Hi,
i found that if user writes in the text field of save dialog name of the file(but application needs directory), getCurrentDirectory() returns correct directory. but if user chooses directory, getCurrentDirectory() returns parent directory but getSelectedFile() returns correct one, so i suggest something like this(it's nasty but it works [i hope;])
JFileChooser dirChooser = new JFileChooser("");
dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (dirChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
java.io.File curDir = dirChooser.getSelectedFile().isDirectory()?dirChooser.getSelectedFile():dirChooser.getCurrentDirectory();

Similar Messages

  • Problem using JFileChooser to select a destination directory

    If you set JFileChooser to DIRECTORIES_ONLY, a user has to single click on the desired directory and click on the approveButton. This works just fine - you get the user's desired directory using getSelectedFile().
    e.g. /home/joeuser/docs
    But if the user double-clicks on their desired directory (a common user practice), the directory is opened AND the directory name is stuck in the File Name: field. If they now click on the approveButton, the returned selected directory is the user's desired directory PLUS the name of the directory again. Not what the user intended.
    e.g. /home/joeuser/docs/docs
    Now, when you keep the File Name: field from being filled in when the user double-clicks on a directory, you have encounter another problem. The approveButton will not activate unless there is something in the File Name: field!
    Does anybody have a solution for the approveButton working with an empty File Name: field?
    Thanks,
    Scott

    I had the same problem. Gave up with JFileChooser and made a directory chooser that only display the diectory structure in a JTree and no file stuff at all.
    Source: http://www.msticky.freeserve.co.uk/JDialog_directoryChooser.java
    to use it; String selectedDirPath=JDialog_directoryChooser.choose(stringTitle, stringInitialSelectedPath)
    returns the selected directory path or null if none selected.
    To make it work alone you'll need to remove my RRException class. If you want to see it in use first download noname.jar from http://www.msticky.freeserve.co.uk
    ps. my site needs testing
    thanks, sticky

  • How to use JFileChooser to select only hard drive?

    I am doing CD update. I am using a component called "JFileChooser". I use this component to let user choose a Hard Drive or Floopy. When I try this component, and if I choose a hard drive and a folder/file, it works. If I only choose hard drive, it will return "My Documents"??? I cann't get hard drive. How to set up JFileChooser so user only choose hard drive and it will return this hard drive. Thanks for help.
    Gary

    Easiest (and maybe most portable) way is to have a specific file in your root directory of the CDROM. You could use java.io.File.listRoots() to get all drives and then pick the one that has your file.
    This will enable the user to copy the CDROM to a hard drive (easier for testing also) or mount a remote CDROM.

  • JFileChooser for selecting directory?

    Hi,
    Are there any better alternative than JFileChooser if I want the user to be able to select a path only, not the filename?
    How could I do it if I necessarily have to use the JFileChooser? The user shall not select any file, only a directory...
    Regards,
    Anders

    Thanks, that helped!
    How could I loop through this directory (this level only, not subfolders), and get a list of all filename of a certain suffix?
    /Anders

  • Get the file directory/location using JFileChooser

    Dear,
    How to get the file directory and it location with using JFilechooser after select the indicate file?
    Thanks

    There is an example at the top of JFileChooser's doc.
    http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/JFileChooser.html

  • Opening a specific directory using JFileChooser.

    Iam using JFIleChooser to select a file is there any way that i the dialog opens at a particular directory say...c:\gallery\imagee...the default dialog opens @ MyDocuments Folder.

    Read the API documentation of the class, especially coustructor section.

  • Using jfilechooser

    hi all,
    how to use jfilechooser to select a file available in another computer of my LAN.
    i used setFileSelectionMode(JFileChooser.FILES_ONLY);with this statement, if i give 'c:' or any local/mapped drive names in text box, it lists the files properly and i can choose the file.
    but, if i give the IP address of another system, i.e like '\\192.168.0.243', it simply accepts this (\\192.168.0.243) as filename and closes the file chooser dialog. if i type the ip address, the file chooser should list the shared drives in that system.  how to do this?
    i tried java.awt.filedialog, but it does this, but supports only single file selection
    thanking you.

    rnoack wrote:
    1) i don't find it amusing that we are even having this conversation, it is immature and i have tried to end it twice now but you always seem to want to have the last word (which would be fine with me if it didn't negatively reflect on my intentions or values)The "problem" in this case is that is that Darryl.Burke is correct. Cross-posting is really rude, it means that you don't care about the time that people spend answering questions. Most frequenst posters at all forums and mailing lists will tell you that.
    Cross-posting and not saying in the post that you are cross-posting is even worse. (Cross-posts are often removed from these forums).
    >
    2) i never did, and never will think that i can tell you want to do or not to do. i was merely offering advice which i feel and will continue to feel regardless of your beliefs is for the best of all related communities.Note that Darryl is a moderator and tried to tell you that you shouldn't cross-post. Most frequent posters would have done that if they had found out that you were cross-posting. The only mistake that Darryl did is that he thought that you were going to listen to his advice even if he didn't make that specific post as a moderator.
    Kaj
    Ps. I also blocked an abusive post.

  • When I try to use the internal hard drive as a scratch disk I get this error "unable to set scratch disk- the selected directory is on write protect or non-writable media.  Any ideas on how to fix this.  It only happens in fcp.

    When I try to use the internal hard drive as a scratch disk I get this error "unable to set scratch disk- the selected directory is on write protect or non-writable media.  Any ideas on how to fix this.  It only happens in fcp.

    By internal, I assume you're referring to your systems (boot) drive. Is it, by chance, a partitioned dive?
    Also…although many people successfully use their systems drives as scratch disks, over time you'll have better results using a dedicated drive for your media.
    Good luck.
    Russ

  • JFileChooser - show files and directories only select directory

    I need a JFileChooser that will display directories and zip files but only allow a directory to be selected.
    basically i need to be able to select a certain directory that contains a certain zip file, so to make it easier to find i want the zip files to show up so i can find the correct directory...
    I tried...
            JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            jfc.setFileFilter(new FileFilter() {
                public boolean accept(File f) {
                    if (f.isDirectory()) {
                        return true;
                    else if (f.getName().endsWith(".zip")) {
                        return true;
                    return false;
                public String getDescription() {
                    return "testing";
            });but that only displays directories in the GUI.
    The javadoc for the setFileSelectionMode() says, "Sets the JFileChooser to allow the user to just select files, just select directories, or select both files and directories. The default is JFilesChooser.FILES_ONLY. " ....which makes you think that setting the file selection mode only alters what can be SELECTED...however it alters what is displayed as well.
    ...any ideas?
    thanks

    This should display the files of interest provided the filter is tuned to do so, and allow you to select either the file of interest or the folder... If you select the file, then folder it is contained is returned.
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new java.io.File("."));
    MyFileFilter filter = new MyFileFilter();
    chooser.setFileFilter(filter);
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    int returnVal = chooser.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {              
      System.out.println(chooser.getCurrentDirectory());
    }

  • JFileChooser with FileSystemView Selecting directory

    Hi,
    I am using JFileChooser and with my own implementation of FileSystemView .
    It is imposable to select a Directory.
    Q1. Do you have any clue about the problem?
    Q2. Is there any way I can add action listener to "Open" button.
    or listen to the events of "Open" button.
    Note:
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);is set.
    Regards,
    Byju

    In the future, Swing questions should be posted to the Swing forum:
    http://forum.java.sun.com/forum.jspa?forumID=57
    If you have to select a directory, not an ordinary file, why not do this first?
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

  • JFileChooser Multiple selection not working on Mac using apple, command key

    I am using Java 1.4 webstart on Mac machine.
    The code is :
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(2);
    fileChooser.setMultiSelectionEnabled(true);
    Once the File dialog opens I have problems in selecting random multiple files. Apple key or command key is used for random selection on Mac. When using either of these keys multiple selection is not possible. Whereas I am able to select files using the shift key by which row selection is possible.
    If this is not a bug and some code changes are to be done it would be fine if somebody can help me out in this.
    If this is a bug in Java 1.4 and is this fixed in Java 1.5?

    All EIDE / ATA (Parallel) devices (drives, opticals) allowed for two such drives or devices on one bus. The old style was "master" and "Slave" but CS is a "smart" approach, however, the master or only device must be on the end position. Also, the end connector will be black, the middle (slave) is gray.
    Eject sends signal to master
    To eject the "slave" device you use a modifier key.

  • JFileChooser Multiple selection not working on Mac using apple key

    I am using Java 1.4 webstart on Mac machine.
    The code is :
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(2);
    fileChooser.setMultiSelectionEnabled(true);
    Once the File dialog opens I have problems in selecting random multiple files. Apple key is used for random selection on Mac. When using the apple key multiple selection is not possible. Whereas I am able to select files using the command key by which row selection is possible.
    If this is not a bug and some code changes are to be done it would be fine if somebody can help me out in this.
    If this is a bug in Java 1.4 and is this fixed in Java 1.5?

    All EIDE / ATA (Parallel) devices (drives, opticals) allowed for two such drives or devices on one bus. The old style was "master" and "Slave" but CS is a "smart" approach, however, the master or only device must be on the end position. Also, the end connector will be black, the middle (slave) is gray.
    Eject sends signal to master
    To eject the "slave" device you use a modifier key.

  • How to get the file path of selected file using JFileChooser

    Hi All,
    I am trying to store the file on my FTP server after selecting the file from the user workstation using JFileChooser. But I am not able to write the file on FTP server or on user local machine where application is running. I am new to this FTP server coding. Can anyone help me in this.
    This is the code I wrote on button click user will able to select the file:
    String filename1;
    File file;
    JFileChooser fc = new JFileChooser();
    fc.showOpenDialog(this);
    file = fc.getSelectedFile();
    filename1 = file.getPath();
    txtFilePath.setText(filename1);
    Below is the code to write the file on my local machine first:
    BufferedWriter outFile = new BufferedWriter( fileName ) );
    outFile.write();
    outFile.flush( );
    outFile.close( );
    Any help will be greatly appriciated

    rp0428 wrote:
    >
    BufferedWriter outFile = new BufferedWriter( fileName ) );
    >
    There is NOTHING named 'fileName' in any of the code you posted so that code can't possibly compile let alone actually run.
    How can anyone help you if you won't post the actual code you are using?Its just a random snippet of other code that is totally irrelevant to this thread if you ask me.

  • Photo problems using "copy selected albums only"

    No one seems to be able to help me at Apple to figure out why if I have PhotoShop Album 2.0 that I can't get the IPod to let me have the choice of "copy selected albums only". The only choice it allows is to copy all photos and I don't want to do that. Would appreciate any help anyone can give me.

    I am not sure what you mean by "set up". It is on the computer I am using with my Ipod and really don't know how the Ipod and it connect/talk to each other. Thanks for at least asking/responding to my question. I have figured out how to get photos from a folder but really would have liked to have used the album feature as they are all organized and I have to mess with them to get them to come out in the folder. Thanks for your time. Nancy

  • When i try to sync photos from iPhoto to my iPad using iTunes,  the "Selected Folders" option is greyed out with only the "All Folders" option available, how do i change this?

    When i try to sync photos from iPhoto to my iPad using iTunes,  the "Selected Folders" option is greyed out with only the "All Folders" option available, how do i change this?

    I add the keyword 'sync' to any photos I want added to the iPhone/iPad.
    Then, during the regular process of syncing the iPhone/iPad with my computer (mac), the photos with that keyword would be copied onto the iPhone, iPad.
    I've been doing it this way ever since I was shown how to; ie after I got my iPhone 2 years ago. And every time the process worked - until the last few weeks. Suddenly it doesn't do it any more.
    I believe I have upgraded software (either iTunes or iPhoto or both) during this period. I try to keep my software updated but have noticed previously that the new version often is disappointing.
    I've not tended to use Smart albums much.
    MM

Maybe you are looking for

  • Can I back up an external hard drive as well as my internal hard drive?

    Hi, I have an Intel Core Duo iMac with a 320GB internal hard drive. I have a 500GB Digital Western external hard drive attached via firewire where I store my iTunes library and iPhoto library. I also use this drive for Time Machine to back everything

  • AGO function in OBIEE

    Hi, I am creating a new logical column on my fact table and would like to use time series function(AGO) to compare trends a month ago. I have a time dimension too. I am using this expression defined in the new logical column properties: Ago("HCBF"."W

  • Expand not working ...

    Hi all. I'm developing a simple scenario where I read employee data and later if expanding, I get the absences it has. For this purpose, I've implemented the GET_ENTITY_SET for both entities and made an association between them. I tested them separat

  • The problem to retrieve and edit Universe from server

    I use CmsResourceService.retrieveUniverse to retrive *.unx from server to local. Then , I use LocalResourceService to load *.blx and cast to RelationalBusinessLayer and throws exception in my program. The exception is NestedException Interface reques

  • When I create some 3D objects in Photoshop CS6 (a good example would be one of a dinner-plate), artefacts always show on the surface of the plate.

    Hello! The title of this message says it all, really (When I typed the above, I thought that I was typing the actual message, since I had been invited to ask a question). So, can anyone please help with this problem. The plates look as if they have w