Results 1 to 2 of 2
  1. #1
    paul is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default How can I make the file ALWAYS save as .txt by default

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

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

    Java 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.

  2. #2
    coco is offline Member
    Join Date
    Jul 2007
    Posts
    39
    Rep Power
    0

    Default

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

    FileNameExtensionFilter (Java Platform SE 6)

    But I don't know how that all work with the save Dialog.
    Greetings.

Similar Threads

  1. Replies: 7
    Last Post: 08-17-2010, 06:00 PM
  2. How to Save/Load Vector to/from file
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:37 PM
  3. [SOLVED] Save the current buffer to file
    By Azndaddy in forum New To Java
    Replies: 2
    Last Post: 03-29-2008, 07:46 AM
  4. save file to database
    By katerinaaa in forum New To Java
    Replies: 0
    Last Post: 08-14-2007, 12:15 PM
  5. How to make a web application as .exe file
    By madhusudhangovindu in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 03:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •