Results 1 to 3 of 3
  1. #1
    masa is offline Member
    Join Date
    Nov 2008
    Posts
    21
    Rep Power
    0

    Default save dialog problem

    hi

    I have a JFIleChooser.showSaveDialog() where I have used setSelectedFile to set a default filename. However, my problem is that when the user selects a subdirectory of the directory the filechooser opened in, then the default filename I set is replaced by the name of the directory the user double-clicked on to open.
    How can I get the file name I set e.g. 'default.txt' to remain in the 'File name' text box when the user is navigating through different directories? I only ever want the filename to change if the user edits it manually.
    Hope you can help

  2. #2
    masa is offline Member
    Join Date
    Nov 2008
    Posts
    21
    Rep Power
    0

    Default save file problem

    here is the code........................

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    public class FileChooseApp extends JFrame implements ActionListener{
    JMenuItem fMenuOpen = null;
    JMenuItem fMenuSave = null;
    JMenuItem fMenuClose = null;
    JTextArea fTextArea;
    JavaFilter fJavaFilter = new JavaFilter ();
    File fFile = new File ("default.java");
    FileChooseApp (String title) {
    super (title);
    Container content_pane = getContentPane ();
    content_pane.setLayout ( new BorderLayout () );
    fTextArea = new JTextArea ("");
    content_pane.add ( fTextArea, "Center");
    JMenu m = new JMenu ("File");
    m.add (fMenuOpen = makeMenuItem ("Open"));
    m.add (fMenuOpen = makeMenuItem ("Save"));
    m.add (fMenuClose = makeMenuItem ("Quit"));
    JMenuBar mb = new JMenuBar ();
    mb.add (m);
    setJMenuBar (mb);
    setSize (400,400);
    }
    public void actionPerformed ( ActionEvent e ) {
    boolean status = false;
    String command = e.getActionCommand ();
    if (command.equals ("Open")) {
    status = openFile ();
    if (!status)
    JOptionPane.showMessageDialog (
    null,
    "Error opening file!", "File Open Error",
    JOptionPane.ERROR_MESSAGE
    );
    } else if (command.equals ("Save")) {
    status = saveFile ();
    if (!status)
    JOptionPane.showMessageDialog (
    null,
    "IO error in saving file!!", "File Save Error",
    JOptionPane.ERROR_MESSAGE
    );
    } else if (command.equals ("Quit") ) {
    dispose ();
    }
    }
    private JMenuItem makeMenuItem (String name) {
    JMenuItem m = new JMenuItem (name);
    m.addActionListener (this);
    return m;
    }
    boolean openFile () {
    JFileChooser fc = new JFileChooser ();
    fc.setDialogTitle ("Open File");
    fc.setFileSelectionMode ( JFileChooser.FILES_ONLY);
    fc.setCurrentDirectory (new File ("."));
    fc.setFileFilter (fJavaFilter);
    int result = fc.showOpenDialog (this);
    if (result == JFileChooser.CANCEL_OPTION) {
    return true;
    } else if (result == JFileChooser.APPROVE_OPTION) {

    fFile = fc.getSelectedFile ();
    String file_string = readFile (fFile);
    if (file_string != null)
    fTextArea.setText (file_string);
    else
    return false;
    } else {
    return false;
    }
    return true;
    }
    boolean saveFile () {
    File file = null;
    JFileChooser fc = new JFileChooser ();
    javax.swing.filechooser.FileFilter filter=null;
    fc.setCurrentDirectory (new File ("."));
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIR ECTORIES);
    fc.addChoosableFileFilter(filter);
    filter = fc.getFileFilter();
    fc.setFileFilter (fJavaFilter);
    fc.setSelectedFile (fFile);
    int result = fc.showSaveDialog (this);
    if (result == JFileChooser.CANCEL_OPTION) {
    return true;
    } else if (result == JFileChooser.APPROVE_OPTION) {
    fFile = fc.getSelectedFile ();
    if (fFile.exists ()) {
    int response = JOptionPane.showConfirmDialog (null,
    "Overwrite existing file?","Confirm Overwrite",
    JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.QUESTION_MESSAGE);
    if (response == JOptionPane.CANCEL_OPTION) return false;
    }
    return writeFile (fFile, fTextArea.getText ());
    } else {
    return false;
    }
    }

    public String readFile (File file) {
    StringBuffer fileBuffer;
    String fileString=null;
    String line;
    try {
    FileReader in = new FileReader (file);
    BufferedReader dis = new BufferedReader (in);
    fileBuffer = new StringBuffer () ;

    while ((line = dis.readLine ()) != null) {
    fileBuffer.append (line + "\n");
    }

    in.close ();
    fileString = fileBuffer.toString ();
    }
    catch (IOException e ) {
    return null;
    }
    return fileString;
    }
    public static boolean writeFile (File file, String dataString) {
    try {
    PrintWriter out =
    new PrintWriter (new BufferedWriter (new FileWriter (file)));
    out.print (dataString);
    out.flush ();
    out.close ();
    }
    catch (IOException e) {
    return false;
    }
    return true;
    }
    public static void main (String [] args) {
    String title="Frame Test";
    if (args.length != 0) title = args[0];
    FileChooseApp f = new FileChooseApp (title);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );
    f.setVisible (true);
    }

    }

    ******************
    JavaFilter.java
    *****************

    import javax.swing.*;
    import java.io.*;
    public class JavaFilter extends javax.swing.filechooser.FileFilter
    {
    public boolean accept (File f) {
    return f.getName ().toLowerCase ().endsWith (".java")|| f.isDirectory ();
    }
    public String getDescription () {
    return "Java files (*.java)" ;
    }
    }

  3. #3
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default I can't find the problem you describe

    I compiled and ran your code.
    It seems to work exactly the
    way you want it to.

    It's possible that on different
    operating systems this code
    works differently.

    I ran this code on a Windows
    System.

Similar Threads

  1. How can i save the data Internally(auto save)
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2010, 09:31 PM
  2. File dialog with save
    By manpreet in forum New To Java
    Replies: 1
    Last Post: 01-02-2009, 06:44 PM
  3. Problem in dialog
    By Sarinam in forum New To Java
    Replies: 5
    Last Post: 07-12-2008, 12:38 PM
  4. Open "Save Page As" Dialog Box
    By Anubha in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-12-2007, 10:27 PM
  5. Replies: 0
    Last Post: 12-06-2007, 07:10 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
  •