JFileChooser selected file

from my code below, the File Name in the dialog/file selection window defaults to the file but once I click on to a directory to change directory the File Name changed to the directory name. How do I set the filechooser to not change the File Name if a directory is clicked? Thanks, jude
File fileSelected = new File("c:\\temp\\newfile.txt");
JFileChooser filechooser = new JFileChooser();
filechooser.setDialogTitle("Save to a text file.");
filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
filechooser.setSelectedFile(fileSelected);
int iStatus = filechooser.showOpenDialog(null);
if (iStatus == JFileChooser.APPROVE_OPTION) {
     fileSelected = filechooser.getSelectedFile();
else if (iStatus == JFileChooser.CANCEL_OPTION) {
     return;
...

I have been able to get the JFileChooser to behave in a more standard way when the user uses the mouse to select files. I have still not been able to find a way to get it to work the way I want if they've typed a file in manually. Does anyone know how to get to the JTextField in the JFileChooser to read the value?
If anyone could hook me up with how to access the JTextField I'd sure be greatful!!!
Here is how I 'fixed' the traversal of folders using the mouse.
I subclassed the JFileChooser and made a local variable
private File lastSelectedFile = null;
I then registered property changed listeners in the constructor for both file selection and directory changes:
this.addPropertyChangeListener(JFileChooser.DIRECTORY_CHANGED_PROPERTY,
(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
processDirChanged(e);
this.addPropertyChangeListener(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY,
(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
processFileSelected(e);
Here are the propertyChangedListeners:
public void processDirChanged(PropertyChangeEvent e) {
if ( lastSelectedFile != null ) {
File newSelectedFile = new File(this.getCurrentDirectory().getPath() + File.separator + this.lastSelectedFile.getName());
this.lastSelectedFile = newSelectedFile;
this.setSelectedFile(this.lastSelectedFile);
public void processFileSelected(PropertyChangeEvent e) {
File selectedFile = this.getSelectedFile();
if ( lastSelectedFile != null && ( selectedFile == null || selectedFile.isDirectory()) ) {
this.setSelectedFile(this.lastSelectedFile);
else {
lastSelectedFile = this.getSelectedFile();
}

Similar Messages

  • JFileChooser selected file changes when folder chages

    Problem with JFileChooser,
    The selected filename change when i change the directories, note that standard windows dialog doesnt behave like this .
    To reproduce: choose "save to file" in process dialog, enter the file name and then change the directory. The file name disappers from the corresponding box. The dialog must keep the entered file name while changing the folder path
    is this a bug ?,
    renjith

    Hi,
    Yes it is!
    http://developer.java.sun.com/developer/bugParade/bugs/4357012.html
    Regards,
    Manfred.

  • 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());
    }

  • How to set selected file in FileChooser showSaveDialog to default file name

    Hi,
    How do I set selected file in JavaFX 2.0 FileChooser showSaveDialog, so I can prompt the user with a suggested default file name?
    I am converting a Java Swing application I wrote a few years ago to JavaFX 2.0.
    In the Swing application, I use setSelectedFile() as follows:
    JFileChooser jFileChooser = new JFileChooser();
    jFileChooser.setSelectedFile(new File(backupfile));
    jFileChooser.setCurrentDirectory(new File(outputDirectory));
    FileFilter filter = new FileNameExtensionFilter("Comma Delimited (*.csv)", "csv");
    jFileChooser.addChoosableFileFilter(filter);
    jFileChooser.setFileFilter(filter);
    filter = new FileNameExtensionFilter("XML Document (*.xml)", "xml");
    jFileChooser.addChoosableFileFilter(filter);
    jFileChooser.setAcceptAllFileFilterUsed(false);
    jFileChooser.setDialogTitle("Export File");
    int returnVal = jFileChooser.showDialog(jFrame, "Export");
    This would show a file chooser with the file name text field pre-populated with a default backup file name.
    I can't find an equivalent in JavaFX.
    Also, in the Swing application, I was able to determine which extension filter was selected at run time using getFileFilter().getDescription() as follows:
    String extension = jFileChooser.getFileFilter().getDescription();
    if (extension.equals("XML Document (*.xml)")) { ...
    I can't find an equivalent in JavaFX.
    Thanks,
    Barry

    You can use the open sequence file method on the application manager (then you only need to wire the file path)
    Rodéric L
    Certified LabVIEW Architect

  • How to save an "Object" in a selected file ?

    Hi,
    I would like to save an object with for example a PlanarImage object type in a selected file from JFileChooser
    Do you know how to do it ?
    I tried this but it didn't work :
    File fChoosen = chooser.getSelectedFile();
    ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fChoosen));
    os.flush();
    os.writeObject(planImage); //write the planarImage
    os.close();
    Thanks a lot !

    Is "PlanarImage" one of your classes? If so, it has to implement the Serializable interface if you want to write it like that.

  • Passing a selected file in a GUI to another class

    Hi guys. I have a GUI set up so that when a file is selected in a directory and the user presses a play button in the GUI..it plays back the sound. (Java sound used.) However i want whatever file was selected to be passed to another class also.In other words..whatever the JFileChooser selects..i want that file info to be passed to another Java class in the package...can anyone help? cheers.

    No...whatever file is selected i just want that
    instance to be sent to another class.Yes it can be done.
    These are the steps.
    1.) Choose the file you want. Once you have chosen the file retrieve the absolute path of that file.
    2.)Call a method of the other class instance to which you want to send the file passing the file path as an argument.
    3.)In the other class retrieve the String path and then use it to create a file object to access it.
    //User chooses a file from file chooser and retrieves the path of that file and stores it in a variable String file_path;
    //Now let other_class_instance be the reference of the other class to which you want to send the files path and let that other class contain a method retrievePath(String path);
    other_class_instance(file_path);
    //The method definition in the other class
    public void retrievePath(String file_path)
            File file_reference=new File(file_path);
            //Now access the file using this file_reference
    }

  • Opening file using JFileChooser with File filtering

    ok guys I am trying set a file filter object to my file chooser it compile but i have a run time error, the error says:
    Exception in thread "AWT-EventQueue-0" java.land.IllegalArgurmentException: Extension must be non-null and not empty
    at javax.swing.filechooser.FileNameExtensionFilter..........
    I have declared
    JFileChooser jfc = new JFileChooser();
    as a global variable
    and in to my ActionListener, ActionPerform the following:
    ActionListener listener = new ActionListener()
              String selection, file, copy;
         public void actionPerformed(ActionEvent e)
    int tipe =0, size = 16;
         String Stile = " ";
         if (e.getSource() == openItem)
              FileNameExtensionFilter filter = new FileNameExtensionFilter("txt");
    jfc.setFileFilter(filter);
              jfc.showOpenDialog(null);
              try
              File f = jfc.getSelectedFile();
              String file = f.getAbsolutePath();
              File f2 = new File(file);
              BufferedReader in = new BufferedReader(new FileReader(f2));
              String text = "";
    String textl = text;
    while( text != null)
                   text = in.readLine() ;
                   textl = textl + text + "\n";
                   System.out.println(text);
    Can some one please tell me whats wrong? and if possible how to fix it? Thank you guys

    A couple of things might help:
    It's a good idea to post the entire stack trace (even though it may appear a bit long). Specifically it helps to know which line in your code triggered the runtime error. You read the stack trace down from the top and the first line you find belonging to your code is a good place to begin looking hard at.
    Another debugging technique is the SSCCE. The idea with this is that you come up with a really brief example illustrating your problem. (ie it compiles, runs and spits out the runtime error). It can be made brief by getting rid of anything extraneous to the problem. A bare button in a frame which when clicked sets up and displays the file chooser should be enough. I describe this as a debugging technique because in the course of constructing such things I often find I isolate - and therefore can see and fix - an error.
    Anyway, sorry this is so general. Maybe someone will come along with a "missing semicolon in line 3"-type solution but, if not, it may be useful.

  • JFileChooser - Selection Highlight lost on Focus Lost

    I have a form with a JFileChooser, a JButton and a JTree. When I select files and that I select another object, the JFileChooser remove the highlight of my selected files (the object still have the list of selected files). So when I reselect the JFileChooser, its look like nothing was selected.
    Is there a setting to keep highlight when the focus is lost?
    Thanks

    U mean these:
    import java.applet.*;
    import java.awt.*;
    public class Cyclus extends Applet {
    public void init() {
    System.out.println("init");
    public void start() {
    System.out.println("start");
    public void update(Graphics g) {
    System.out.println("update");
    public void paint(Graphics g) {
    System.out.println("paint");
    public void stop() {
    System.out.println("stop");
    public void destroy() {
    System.out.println("destroy");
    the problem is that these methods are first called when the event has happent. I need it WHEN it happens....

  • Selected file cannot be linked because its type (video) does not match the original file's type

    I've been using Premiere Elements 9 for a couple of years now, and a few months ago I began having problems importing AVI files created from a BlackMagic Intensity Pro capture card.  (These issues were not previously occurring, and since they surfaced, I have exhausted the various settings for the BlackMagic card, used various software such as BlackMagic's own Media Express as well as the freeware VirtualDub, and still have the following problems.)
    In addition, these AVI files play just fine, in their entirety, using Windows Media Player and in Quicktime.
    The problem (or possibly two, but I suspect they could be related) is that most of the AVI files I attempt to import will either (1) import all video and NO audio, or (2) import ONLY a short segment of audio and video, and the length that Elements imports varies.  Usually only in the range of a minute or two.  The rest of the file is not imported at all.
    After much digging in forums, and testing various settings in trying to figure out the issue, I decided to back up all of my files and completely wipe all hard drives and start over with a fresh installation of Windows and Premiere Elements 9.  I haven't installed much else yet, as I wanted to first find out if the reinstallation improved things with Elements.
    For the first project I created in Elements after the reinstallation, Elements successfully imported an entire AVI that was nearly 2 hours long, audio and video.  This was a file that previously would not import properly into Elements.  This suggested that my earlier installation of Elements must have been corrupted somehow.  At this point I was very hopeful that all was well.
    I proceeded to edit the video as I had wanted, saved the project, and exported it to an MPEG format file.  I had originally wanted to export it as an MOV, but I hadn't yet installed Quicktime, so Elements couldn't do that just yet.  The MPEG export was successful and plays fine.
    However, I then installed Quicktime, and once it was finished, I restarted the PC and reloaded the same project.  This time, Elements claimed the media was offline.  I double-clicked on a segment of the video, and directed Elements to the raw file, but then an error message popped up:  "The selected file cannot be linked because its type (video) does not match the original file's type (audio and video)."  This was the same file I had JUST edited and of which I created an MPEG.
    I tried creating a new project and the "Get Media" function to import the AVI again, but this time only the video imported with NO audio.  (This would explain why the error message said what it did, but not why the audio wasn't imported.)
    What in the world is going on with Elements???  I am baffled since the ONLY task I have done on this machine after a complete reinstallation of both Windows and Elements is edit one AVI.  I find it very hard to believe that just installing Quicktime caused this.
    Some info on my system:
    System:  Asus P6T w/ Intel i7 920 and 12 GB memory
    OS: Windows 7 Ultimate
    Video Software:  Premiere Elements 9
    Capture Card:  BlackMagic Intensity Pro (PCI-e)
    Capture Software:  AVIs recorded with either BlackMagic Media Express or VirtualDub (only prior to the reinstallation, as neither of these programs are currently installed)
    I'm getting to the point with Elements that I might need to just abandon it and find something else that is more reliable.  Anybody have suggestions to help with this, so I won't have to migrate?

    mtnsports
    We think that we need to start off with definiting the properties of these .avi files that you are importing into Premiere Elements 9.0/9.0.1 (assumed that you are working for the required 9.0.1 Update).
    You say that the "avi" was created from a "BlackMagic Intensity Pro capture card". What are the properties of this .avi that you imported into Premiere Elements? Not all .avi are created equal. What is the video and audio compression, frame size, frame rate, interlaced or progressive, pixel aspect ratio, typical duration?
    Based on the properties of the video what have you been setting as the project preset?
    Let us start here.
    More later.
    ATR

  • Edit in Photoshop CS4... does not open the selected file.

    Sorry if this is an old topic, but I can not seem to find the answer and I was hoping someone here might know.
    When in Lightroom (2.2 or 2.3) if I select "Edit in Photoshop CS4..." or any of those dropdown commands, Photoshop opens....and that is it. The selected photo from Lightroom is NOT opened/merged/panoramaized. duuh. frustrating.
    So I re-installed Lightroom, updated to the 2.3, re-installed CS4. all with the same effect that Photoshop opens, but the selected file is NOT opened.
    Has anyone experienced this? Any solutions?
    Thanks in advance.

    When you remove the obvious,
    that which is left must therefore be the truth -- Sherlock Holmes
    Short version:
    When I created a new lightroom catalog, and imported a single, standalone file, It worked fine. But when I imported to the new catalog, any photos from my existing catalog, it would fail.
    Turns out that I was doing something a bit out of the ordinary with the IPTC Copyright field. In order to make my copyright information look "different" when I would export a file.
    One would normally put in that field something like "© 2009 someone". I discovered a few months ago that I could make this field display as a multi line entry.
    ie.
    if you wanted to put XY into the copyright field, but wanted it to display as:
    X
    Y
    all you would have to do is to put a carrage return between the two characters. (hint ctl^enter). Now when you displayed the "copyright" in an output file, it will display
    vertically instead of all the characters in a horizontal line.
    After being frustrated by this for a couple of weeks, I simply started removing every option that I might have applied to photographs that I would import with Lightroom. One of the last ones was the "vertical" information I put in the copyright field in a Preset. (when you remove the obvious...).
    Funny thing is that no program really cares about the formatting of that field, or if it has multiple lines in the data, or special characters.......It even opens up just fine in Photoshop / Gimp / PSE / etc. with no problems.
    But, if you have multiple lines of data in the Copyright Field (and I suspect others as well),
    AND you want to open up the file from Lightroom in Photoshop, using the standard menu item of " Edit with Photoshop CS4", for some reason Photoshop will start, but never open the designated file. I would expect some parsing something just giving up after it encounters a CR.
    But.....All is well that ends well. Now I can quit obsessing, and move on to something else...
    Best,
    Jim

  • Is there a way to sync to an ipad or iphone selected files from a secondary itunes without erasing everything on the mobile device.

    Is there a way to sync jujst selected files to an ipad or iphone from an itunes program that is not the one that is primarily used for that specific remote device?
    I sync my ipad to my desk top but also have a laptop that has some files that I want on my ipad but don't want to erase everything to get them on.

    Not really.  iOS devices were designed to sync to one iTunes account, not multiple accounts.  Simply email what you want to your Desk Top and transfre them to your iTunes library on that computer.  Or get a Dropbox account.  You can download Dropbox on your iPad and have dropbox on your Desk Top and Laptop.  You could access files, music, pictures, etc., you put in your Dropbox account from any device.  You get 2GB free storage, but you can purchase additional storage if you need it.

  • Combine files to PDF and selecting files one at a time - Adobe CreatePDF online application

    Are you able to select up to 10 files at one time through the "select files" browser window?  The instructions at https://www.acrobat.com/createpdf/en/merge-combine-pdf-files-online.html seem to indicate that you are able to select up to 10 items and then convert and combine. 
    "1 Sign in to your CreatePDF account online
    2 Click on Combine files to PDF, then Select Files
    3 Choose one or more files to merge into a single PDF
    4 Click Open"
    At this point I am only able to select files 1 at a time, which means steps 3 and 4 repeat up to 10 times if you are combining the maximum 10 files at once.  I have tried using shift select and ctrl select to no avail.
    Thank you for any help on this issue.

    I think I found my answer at a different set of instuctions at http://helpx.adobe.com/acrobat-com/kb/using-createpdf.html.  This set of instructions clearly indicates that files will be selected 1 at a time.
    Click Combine Files To PDF.
    Click Select Files.
    Select one of the files that you want to combine.
    Click Open.
    Click Add Files and select additional files to combine. Click Open."
    If anyone has any ideas on how to more quickly combine files, any help would be greatly appreciated.

  • Selected file failed to open as a PDF error .. on ARES 7.0

    I am trying to enable some reader features using Adobe LiveCycle Reader Ex 7.0 but when I upload the pdf file i got this message
    "Error 442: Selected file failed to open as a PDF 'com.adobe.document.pdf.PDFOperationFailure: IDL:com/adobe/document/pdf/PDFOperationFailure:1.0'."
    when I open this fiel by Adobe Reader 8.0 I got this message
    "This document contained certain rights to enable some features in Adobe Reader. This document has been changed since it was created and these rights are no longer valid. Please contact the author for the original version of this document."
    is there any right should be assigned during the creation of PDF to avoid this error? i am using these products
    Adobe LiveCycle Reader Ex 7.0
    Adobe Acrobat 7.0
    Adobe Reader 8.0
    thank u;

    I am trying to enable some reader features using Adobe LiveCycle Reader Ex 7.0 but when I upload the pdf file i got this message
    "Error 442: Selected file failed to open as a PDF 'com.adobe.document.pdf.PDFOperationFailure: IDL:com/adobe/document/pdf/PDFOperationFailure:1.0'."
    when I open this fiel by Adobe Reader 8.0 I got this message
    "This document contained certain rights to enable some features in Adobe Reader. This document has been changed since it was created and these rights are no longer valid. Please contact the author for the original version of this document."
    is there any right should be assigned during the creation of PDF to avoid this error? i am using these products
    Adobe LiveCycle Reader Ex 7.0
    Adobe Acrobat 7.0
    Adobe Reader 8.0
    thank u;

  • Error message: "Unable to prepare the selected file for editing"

    There is another similar post. <br /><br />I have imported a bunch of Nikon NEF raw files into Lightroom. I can work them without any problem, but when I right-click the image to edit it further in Photoshop CS3. I get this message:<br /><br />i "Lightroom was unable to prepare the selected file <filename.NEF> for editing. It will not be opened."<br /><br />Photoshop has no trouble opening these NEF files by itself, but Lightroom can't hand it them off. Anyone know why this is? Nothing I can find in the help file or elsewhere here.<br /><br />Stan

    Sean, if they are off-line, would any of this Xmp stuff make any difference? Don't they need to be brought On-lie to edit? And where would the Xmp's go with them off-line?
    Don
    Don Ricklin, MacBook 1.83Ghz Duo 2 Core, Pentax *ist D
    http://donricklin.blogspot.com/

  • Help! Can't save to disc/backup my iTunes! NO OPTION to save OR back up to disc my iTunes Library appears in the toolbar when I select 'File', then 'Library'. Just 'Organize Library...', 'Export Library...', 'Import Playlist...', and 'Export Playlist...'

    Help! I can't save to disc OR backup my iTunes Library! iTunes support instructions are to simply select 'File', then 'Library', then select 'Back Up to Disc', but when I try to the only options that appear are 'Organize Library...', 'Export Library...' then below those are 'Import Playlist...' and 'Export Playlist...' (which appears in the list, but I cannot select). No option to save my library appears at all. Just those four. I've had no problems syncing, playing, adding or purchasing with my account and my iTunes version is up to date. Can anyone tell me what the problem is and what I should do so I can save my library?

    See iTunes for Windows- Moving your iTunes Media folder

Maybe you are looking for

  • HELP! Can No longer edit certain files in DW CS5

    I was editing these fine for the past two days and i reuploaded them to the ftp server and had to make a change to one and now they are all greyed out and impossible to edit here is code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

  • Crystal report : Export to Text format from BI Launch pad

    Hi, We are facing issue in exporting the Crystal report to text format option from BI Launch pad view mode. Currently BI launch pad doesn't support export to Text format option for crystal report in view mode. But export to text format option is feas

  • (Win 8.1) x230 Speaker volume strenght / Realtek Audio Driver may be unnecesary

    Hi This is more a poll/discussion rather than a question. I wanted to share with you that I have a x230 and as soon as I installed Win8.1 x64 audio worked fine with "Intel High Def Audio" driver. I though sound was kinda weak (I know this is a tiny l

  • Raise statemnt use in exception block

    in one procedure i found code like this begin.. exception when NO_DATA_FOUND then dbms_output.put_line('error in data fetching'); when others then raise; end; in when others if we use raise as mentioned above what will happen when other than no data

  • ISE 1.1.3 provisioning problem for the first DOT1x connection

    Hello all, I am wondering how a wired dot1x client can get the NAC agent downloaded for its very first connection from ISE ? Should the Agent be installed before the first connection ? I'have set up ISE 1.1.3 for provisioning (files have been downloa