Suggest file name in JFileChooser?

I'm a newbie (student) to both Swing and Java in general. In using JFileChooser, It occurred to me that it would be nice to offer a suggested file name when saving files.
For those of you who have used MS Word, when one saves a new file, Word offers the document title or a few words of the document's first line as a suggested file name. This suggestion is displayed in the file name textbox of the save file dialog.
Is it possible to do this sort of thing in Java and with JFileChooser? If so, how?
Thanks in advance for any feedback.

JFileChooser  fc = new JFileChooser(".");
fc.setSelectedFile(new File("AnyFileNameYouWant.txt"));

Similar Messages

  • Excel 2010 missing default suggested file name in Save As dialog box

    I’m using Excel 2010 running on Windows 7 (32 bit).  When I open a certain file which produces the following message: "A file is in a different file format than its extension indicates", and then go to “Save As” (or “Save”) the
    file, the default suggested file name is missing (blank).  This didn't occur in previous versions of Excel. 
    In previous versions, Excel would automatically populate the original file name in to the File Name field.
      I have searched all over the net, looking for a way to change Excel so that it will once again populate the file name in this situation. 
    To reproduce:
    Create a new blank workbook
    save as type “Web Page”, (i.e. File name:  “Blank Example.html”)
    Close workbook
    In windows, rename file from .html to .xls  (i.e. rename “Blank Example.html” to “Blank Example.xls”)
    In Excel open renamed file (i.e. “Blank Example.xls”) and click “Yes” when prompted with the “A file is in a different file format than its extension indicates” message.
    Do a “Save As” and you will notice that the File Name is blank. In previous versions, this field would contain the current workbook file name
     (i.e. “Blank Example.xls”)
    Any help will be greatly appreciated.

    Hi Jaynet,
    In order to re-produce this, you need to answer "yes" to the rename file prompt and then continue with step 5 (above).
    The reason for this is not an exercise in futility - I assure you.  At my work and elsewhere, when web developers have created features to permit the end user to save web data in Excel format, often times the Excel files are saved locally in Excel's
    html format (but with the .xls
    extension). 
    (I actually prefer the .xls
    extension, because it is easier to just double-click the file to open in Excel, rather than to select the open-with and then select Excel. a file with the .html extension will default open in your default browser. Now, I could change my default program
    for the .html extension, but that would only solve a part of the problem and would not really address the bigger issue and that being that Microsoft changed a behavior in Excel and may not even be aware that it was a much used feature. )
    To continue, when I go to open the resulting Excel file, I am prompted with the message that the file type does not match the extension (which is fine and not bothersome to me).  It's at this point when I go to save the file that I get really annoyed.
    In previous versions of Excel, the default file name would be pre-filled with the current name of the file and the default file type would state that it is a Web html file.  I would just change the file type to Excel Workbook and hit enter to save.
    I would be prompted with "Are you sure you want to overwrite your existing file?" message and I would click "yes" and that would be that.
    However, in Excel 2010, because the default file name is blank, I then need to re-type the name into the field to save the file. 
    Any help is greatly appreciated.
    Thanks

  • Easy -- default file name in JFileChooser

    Hello,
    I have a JFileChooser dialog ready and working. I select the directory, then input the file name and press Save.
    The enhancement I would like to add to the dialog would be displaying a default name already in the FileName field once the dialog is opened. For instance "REPORT_ONE_" and then I would only add some text to the end of the default name. This would save me from typing the "REPORT_ONE_" all the time as this would already be displayed in the filename field.
    I could add the "REPORT_ONE_" to the filename later, when saving the report. However appending anything to the filename "behind the scenes" is not acceptable by the customer. They need to see the filename, not guess it.
    Looking forward to your help, fellow forum users.
    Thank you!
    aulo

    Thank you, 5p given.
    However this is what I execute and in my file chooser dialog the filename field is still blank.
      ExampleFileFilter filter = new ExampleFileFilter();
        filter.addExtension("html");
        filter.addExtension("csv");
      JFileChooser chooser = new JFileChooser(new File("REPORT_ONE_"));
        chooser.setDialogTitle( "Save Report" );
        chooser.setFileSelectionMode(chooser.FILES_AND_DIRECTORIES);
        chooser.setFileFilter(filter);
        int returnVal = chooser.showSaveDialog(this);
        ...It is in try-catch, no errors are returned.
    Operating System: WindowsXP
    Java Version: 1.3.1

  • Save As with suggested file name and folder

    I would like the user to be able to press a save button on the pdf and the "Save As dialogue box" pops up pre-populated with a file name and location choosen. The user can then choose to name the file something otherwise or select a new location. I would just like to save them the steps of copying the field data and finding the correct folder (deeply buried on server) however to place the finished document in.
    This doesn't seem to require a trusted function, but how do I suggest the file name based on a field name in the document. And how do I suggest the folder the document should be saved into?
    Thanks in adavance.

    Hi,
    you need a folder level script to use the browseForDoc method.
    http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/js_api_reference.pdf#page=10 4

  • Programaticly changing the (suggested only) file name in the save prompt when signing a document?

    When you sign a document in Adobe X (or XI), the system says it needs to save the file first.   Is there some way to change the (suggested only) file name to a different value for that save?
    Right now, people open a blank form - lets call it formX.pdf.   They fill it in and sign it.  The suggested file name for the save is FormX.pdf.   I can generate a unique file name which i'd like to put in place of the suggested default...

    AFAIK, you can't do it with code. Your best option might be to use a form field to show a message to the user when they sign the document with that information.

  • When saving to SharePoint 2010, we want Word to use document title for file name

    We have a template and custom Document information Panel.
    In the panel we select a few values to filter the selection list of document numbers. After selection of document number the title is populated from an external list of document numbers with titles.
    On initial save, the suggested file name provided by word is not the title that is already populated. If we cancel, and click save a second time the title is now the suggested file name.
    Is there a workaround or fix for this issue either for sharepoint or word? To make it use the title information on the initial save attempt?
    Thanks in advance,
    Chris.

    Hi  Chris,
    Do you use SharePoint Designer Workflow to get and  populate the document’s file name?
    If so, the  issue is caused by  delayed Workflow.
    For a workaround, you can create an event  receiver  with ItemAdding method for getting and populating the document’s file name.
    For more information, you can refer to the article:
    http://msdn.microsoft.com/en-us/library/gg749858(v=office.14).aspx
    Best Regards,
    Eric
    Eric Tao
    TechNet Community Support

  • JFileChooser does not display japanese file names

    I am using JFileChooser to load some files in my application. I have some files which have japanese file names. When I try to load these files it does not even display these files for me to choose and then load them. Any ideas?

    Hi,
    I am trying to pick up data from an excel sheet and insert them into a database. However, once updated, they end up with '?' values.
    I have tried to make font.properties.ja as the default font.properties file, which failed to display Japanese characters.
    Any suggestions?
    This is a part of my code:
    String sJIS = "";
    sJIS = new String(cell1.getStringCellValue().getBytes("8859_1"), "JISAutoDetect");
    String sUTF = "";
    sUTF = new String(cell1.getStringCellValue().getBytes(), "UTF-16");
    System.out.println("sUTF" + sUTF + " " + "sJIS" + sJIS);
    Thanks,

  • Make "File name" and "Files of type" fields read-only in JFileChooser

    I try to make the "File name" and "Files of type" fields read-only in JFileChooser dialog. Anybody
    knows how to do? Thanks.

    You mean so the user can't choose the name of the file to open or save? Not much point in even using a JFileChooser, then, is there? Or did I misunderstand the question?

  • How to position the cursor in File Name area in JFileChooser??

    Hi,
    I have been trying to position the cursor directly to filename area in JFileChooser. But no success.
    It positions itself at the directory path by default.
    How should I achieve this?
    The problem is that I do not know the name of this
    component used for File Name area hence difficult to
    use "requestFocus() or getFocus()" like methods.
    Thanks for your help.
    -Manoj

    Somewhere I saw a tip that explained how to do it, but I didn't keep a link to it. But basically it's like this: JFileChooser is a Container, so getComponent(n) will return the n-th component in the JFileChooser. One of them is the one you want to give the focus to, and I expect it will be a JTextField. A little experimentation should tell you which index you need to use.

  • How to set file name in a JFileChooser to be some default String?

    Dear all,
    I am tring to make my JFileChooser more user-friendly, so every time that a JFileChooser show up, I would like to fill the file name with some default String, instead of just leaving them blank.
    Does anybody know how to do this?
    Many Thanks!

    look up the set...() methods of the JFileChooser api docs

  • JFileChooser "File Name" field

    Hi,
    I am trying to use a JFileChooser as a component within a JDialog, and everything works fine, except that I do not understand how I can get the value of a filename typed into the "File Name:" field on the form. I've read the "JFileChooser" Java doco about five times and I can't see anything that would give me access to this field. GetName() appears to be a red herring.
    Anyone got any ideas?
    Thanks,
    Tim

    Thanks for your swift reply.
    Unfortunately perhaps I wasn't quite clear - I have added the JFileChooser as a component to a JDialog, not run it standalone via showDialog() or similar.
    I am trying to get the user to select a directory and type a file name that a new file can be saved into.
    Here is the code that displays the chooser:
       private JPanel saveFilePnl(String folder)
          JPanel panel = new JPanel(new BorderLayout());
          panel.add(new JLabel("<html>Select the folder to save the new sequence into,<br>" +
                               "and type in an appropriate File Name:<br>"),"North");
          saveFileChooser.setCurrentDirectory(new File(folder));
          SimpleFileFilter filter = new SimpleFileFilter("xml", "Nucleotide Sequences"); 
          saveFileChooser.setFileFilter(filter);
          saveFileChooser.setControlButtonsAreShown(false);
          saveFileChooser.setBorder(BorderFactory.createCompoundBorder
                                (BorderFactory.createEtchedBorder(),
                                 BorderFactory.createEmptyBorder(8,8,8,8) ));
          panel.add(saveFileChooser, "Center");
          return panel;
    }This panel is added to a card on a CardLayout JDialog. After the user selects a folder and types in a filename I want to verify that they have done so before moving on to the next card. So I need to ensure that there is a name in the "File Name:" field on the chooser.
    Thanks,
    Tim

  • Can JFileChooser file name field return all entered chars

    In JFileChooser, if I want to get all entered characters from file name field, is there a method I can call?
    I used method chooserUI.getFileName() or chooser.getSelectedFile(). These two methods returned texts which missed back slash and forward slash. I want to get everything what I entered in file name field. I searched and read related JFileChooser topic, but there isn't a topic about this. Can any of you know what I can do?
    Thank you very much.

    JFileChooser isn't for entering text, it's for selecting files. If you put for example "abc/def" in the filename box, it's going to interpret that as "file def in directory abc" and your getFileName() will return "def". (You'll notice that getFileName() doesn't return the directory path, just the filename part of it, right?)

  • Mp3 will not rename - reverts back to original file name. Any suggestions?

    I have a bunch of mp3 files that I would like to rename the original file name. Itunes only allows me to change metadata, but to fix broken names and keep organized, I need to change the file name.  Once I rename the file, it shows the new name, and then upon playing it, it reverts back to the original name. I do not have any locked permissions. I have parallels and tried to rename them on the windows 7 side but same effect.  I tried to shut all programs off so the file was not in use. I even tried to export it as a acc file type.  I am exhausted looking for a solution, this is why I am using this forum as a last ditch effort. Please help.

    As far as I am aware, iTunes does not rename files (at least nto my older version I use).  Yes, it will insist upon the files being in certain folders which is structured from the metadata, but the files themselves keeep whatever name you use.
    I suggest this be moved to an iTunes forum (you choose for Mac or for Windows).  I have never seen anybody else complain about this in the past.
    One question, do you subscribe to Match?  Is there anything else using these files, some third party software?

  • JFileChooser file name extention queistion

    hi i would like to know if it is possible to place a file extention in the filename section when you open the save dialog. So as when the user types a file name in the file extention is added automaticly to only allow the user to save in .png. My code is as follows.
    private void screenshot()
              /** currently no dialog to confirm action as remains
              on screen when saving screenshot! **/
              int status;
              JFileChooser fc = new JFileChooser();
              fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
              try {
                        Rectangle rect = new Rectangle(Toolkit.getDefaultToolkit ().getScreenSize ());
                        BufferedImage image = new Robot().createScreenCapture(rect);
                        try {
                             fc.addChoosableFileFilter(new PNGFilter());
                            status = fc.showSaveDialog(information);
                            if(status == JFileChooser.APPROVE_OPTION)
                            File selFile = fc.getSelectedFile();
                            ImageIO.write(image, "png", selFile);
                            JOptionPane.showMessageDialog(information, "File saved to "+ selFile);     
                        } catch (IOException e) {
                                  Error.SaveError(e);
                        catch (AWTException e) {
                             Error.AbstractWindowToolkit(e);
    }

    You can always check selFile's name, and if it
    doesn't end with .png replace it
    with a new File with the same name, but with .png
    added on.Thanks for that. I have done just that and it works great now.
    if(getExtension(selFile).equals("NO_EXTENSION"))
                               selFile = new File(selFile.toString() + ".png");
                           }

  • JFileChooser File Name

    Hi!
    I'm using a JFileChooser to save in a local folder a file stored in my database and I'd like to set the File Name when I open the FileChooser, do you know how to do it ??
    Thanks !!
    TLLI

    JFileChooser.setSelectedFile()?

Maybe you are looking for

  • Error while starting WLI server on Solaris Machine

    Hi While starting WLI server using startWLI.sh command I am getting following error. <i>Exception in thread "main" java.lang.NoClassDefFoundError: weblogic/Server</i>:-( Please help me for rectifying this error. Thanking you. Amol

  • Disk too slow / System overload HELP?

    I'm really hoping someone can help me out with this!: I purchased a G4 PowerBook around six months ago, with Logic Pro pre-installed. Since then, I have mixed and mastered lots of music that was recorded on a previous system and imported. I was runni

  • Z97-G45 HDMI port doesn't work

    I have Z97-G45 the HDMI doesn't work what's the problem? should i install driver for it?

  • Suddenly i can't get sound on several sites. what to do?

    suddenly I can't get sound on several sites. What to do?

  • IWEB not uploading URL correctly

    For several days I have been struggling with IWEB to get it to load to my url through ftp. Here is the thing....IWEB makes you do a "site name" and when you do that it becomes the landing URL for example I want people to type in www.photocandy.biz an