ho questo JFC:
a seconda del filtro scelto deve essere richiamato un metodo piuttosto che un altro.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; } } }
il problema è che nn riesco a sistemare in modo tale che funzioni sia il richiamo al metodo giusto, sia l'eventuale Message.

Rispondi quotando