Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [Java] FileChooser condizionale

    ho questo JFC:
    codice:
        private static File f = null;
    
        public void salva() throws IOException, WriteException {
            JFileChooser fc = new JFileChooser();
            fc.setDialogTitle("Save");
            fc.setApproveButtonText("Save");
            fc.setApproveButtonToolTipText("Approve file");
    
            FileFilter xlsFilter = new GenericFileFilter("File *.xls", "xls");
            FileFilter txtFilter = new GenericFileFilter("File *.txt", "txt");
            fc.addChoosableFileFilter(xlsFilter);
            fc.addChoosableFileFilter(txtFilter);
    
            int returnVal = fc.showSaveDialog(fc);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                f = fc.getSelectedFile();
                FileFilter selectedFilter = fc.getFileFilter();
                if (f.getName().indexOf('.') == -1) {
                    if (selectedFilter == xlsFilter) {
                        f = new File(f.getPath() + ".xls");
                    } else if (selectedFilter == txtFilter) {
                        f = new File(f.getPath() + ".txt");
                    }
                }
                if (f.exists()) {
                    String msg = MessageFormat.format("The entry ''{0}'' already exists.\nDo you want to replace it?", new Object[]{f});
                    int r = JOptionPane.showConfirmDialog(null, msg, "Confirm", JOptionPane.YES_NO_OPTION);
                    if (r == JOptionPane.NO_OPTION) {
                        return;
                    }
                }
            }
        }
    
        private static void salvaTxt() throws IOException {
            FileWriter file = new FileWriter(f, false);
            PrintWriter out = new PrintWriter(file);
            String temp;
            for (int row = 0; row < FormMain.getTable().getRowCount(); row++) {
                temp = "";
                for (int column = 0; column < FormMain.getTable().getColumnCount(); column++) {
                    temp = temp.concat((String) (FormMain.getTable().getValueAt(row, column)) + " ");
                }
                out.println(temp);
            }
            out.close();
        }
    
        private static void salvaXsl() throws IOException, WriteException {
            ExportJava test = new ExportJava();
            test.setOutputFile(f.toString());
            test.write();
        }
    
        private class GenericFileFilter extends FileFilter {
    
            private String descrizione;
            private String finale;
    
            public GenericFileFilter(String descrizione, String estensione) {
                this.descrizione = descrizione;
                finale = "." + estensione;
            }
    
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;         // Accetta sempre le directory!
                }
                String name = f.getName().toLowerCase();
                return name.endsWith(finale);
            }
    
            public String getDescription() {
                return descrizione;
            }
        }
    }
    a seconda del filtro scelto deve essere richiamato un metodo piuttosto che un altro.
    il problema è che nn riesco a sistemare in modo tale che funzioni sia il richiamo al metodo giusto, sia l'eventuale Message.

  2. #2
    nessuno ha un'idea??

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.