Come faccio a far si' che venga aperta quella finestrella dove c'è la lista file e directory per l'apertura e per il salvataggio?
Come faccio a far si' che venga aperta quella finestrella dove c'è la lista file e directory per l'apertura e per il salvataggio?
Devi instanziare un JFileChooser, ti restituisce il file selezionato.
Consulta le api in linea, ora nn ricordo esattamente come funzica.
Ciao
Windows Xp
[Java]
[PHP]Notepad++
[Fortran90-77] elf90 g77
[C++ /WinAPI] DevC++ VisualC++
Preso dalle API di java
codice:JFileChooser chooser = new JFileChooser(); // Note: source for ExampleFileFilter can be found in FileChooserDemo, // under the demo/jfc directory in the Java 2 SDK, Standard Edition. ExampleFileFilter filter = new ExampleFileFilter(); filter.addExtension("jpg"); filter.addExtension("gif"); filter.setDescription("JPG & GIF Images"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(parent); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); }
Lang=Java
Ambiente = Eclipse forever
Ubuntu & Win XP Pro
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("txt");
filter.setDescription("Text document");
jfc.setFileFilter(filter);
int returnVal = jfc.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
jfc.getSelectedFile().getName());
mi dà 3 errori:
Editor.java:66: cannot resolve symbol
symbol : class ExampleFileFilter
location: class Editor
ExampleFileFilter filter = new ExampleFileFilter();
^
Editor.java:66: cannot resolve symbol
symbol : class ExampleFileFilter
location: class Editor
ExampleFileFilter filter = new ExampleFileFilter();
^
Editor.java:70: parent is not public in java.awt.Component; cannot be accessed f
rom outside package
int returnVal = jfc.showOpenDialog(parent);
^
3 errors
Il parametro parent(di tipo Component) indica il componente sul quale vuoi far comparire il menù , ad esempio se sei in un frame puoi scriverci this, altrimenti se vuoi rendere indipendente il menù metti null.
No scusa , la classe ExampleFileFIlter puoi toglierla dall'esempio.
Lang=Java
Ambiente = Eclipse forever
Ubuntu & Win XP Pro
codice:int returnVal = jfc.showOpenDialog(parent); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + jfc.getSelectedFile().getName());
Lang=Java
Ambiente = Eclipse forever
Ubuntu & Win XP Pro
Grazie!!!
Come faccio a far si' che se il file e' testuale venga caricato nel JTextArea?
if(jfc.getSelectedFile().getExtension() != "txt"){
System.out.println("Errore!!!");
}
puo' andare?
Un volta che hai ottenuto il nome e il path del file
devi crearti un STream di input e leggere dallo stream.
Se vuoi fari si che vengano visualizzati solo file di testo devi crearti una classe che estenda FileFilter.
Cmq ti consiglio di dare uno sguardo alle API di java
http://java.sun.com/j2se/1.4.2/docs/api/
dovresti sempre averle sotto mano quando programmi.
Lang=Java
Ambiente = Eclipse forever
Ubuntu & Win XP Pro
Ah ok. Thank you
ciao