Web Analytics Made Easy -
StatCounter Java - default filename.ext in saveDialog - CodingForum

Announcement

Collapse
No announcement yet.

Java - default filename.ext in saveDialog

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Java - default filename.ext in saveDialog

    I have a save dialog box that seems to work except for a couple issues:

    1) when the save dialog first opens I want a default filename "testing.txt" to appear in the filename field.
    How do I make this happen?
    (currently trying "setSelectedFile(new File("FileName"));", but no luck yet)

    2)How can I make the file ALWAYS save as .txt by default?

    any suggestions are greatly appreciated.

    below is the code for my save button:
    Code:
                String messageCont = outTextArea.getText();
                
                /*create file chooser*/
                final JFileChooser fc = new JFileChooser();
                /*attache the file chooser to the Frame and detect OK or Cancel*/
                int returnVal = fc.showSaveDialog(HanoiFrame.this);
                //fc.setSelectedFile();
                File file = fc.getSelectedFile();
                //File file = fc.setSelectedFile(new File("testing.txt"));
    
                
                
                if (returnVal == JFileChooser.APPROVE_OPTION) {
    
                    try {
                        /*if OK write the file*/
                        BufferedWriter output = new BufferedWriter(new FileWriter(file.getPath() + file.getName()));
    
                        output.write(messageCont);
                        output.close();
                        
                    } catch (IOException ex) {
                        
                        ex.printStackTrace();
                    }
                } else {
                    
                    /*cancel*/
                    return;
                    
                }
    Thanks.

    YaK

  • #2
    Now, I haven't done this before, but you may have to use a File Filter such as:

    FileNameExtensionFilter

    But I don't know how that'll work with the save Dialog.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

    Comment

    Working...
    X