Set default saving extension with JFileChooser

后端 未结 5 1093
借酒劲吻你
借酒劲吻你 2021-02-07 10:12

I\'m trying to save a file using JFileChooser. However, I seem to be having some trouble with it. Here\'s my code:

    if (e.getSource() == saveMenu         


        
5条回答
  •  一生所求
    2021-02-07 10:30

    How about something like this:

        else if (e.getSource() == saveMenu) {
            int returnVal = chooser.showSaveDialog(Simulator.this);
    
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
    
                String fname = file.getAbsolutePath();
    
                if(!fname.endsWith(".xml") ) {
                    file = new File(fname + ".xml");
    
                if(!file.createNewFile()) {
                    /*check with user??*/
                }
    

提交回复
热议问题