JFileChooser?

1 resposta
J

Galera acho q é esse o nome pelo que pesquisei… eu gostaria que voces me ajudassem…
quero q gando eu clico no botão abra aquel caixinha de salvar… tipo quando vai salvar algum
documento… a real eu queria uma para abrir um arquivo… tipo gostaria de colocar a foto dos
cadastrado no meu pequeno aplicativo… como faço isso?

grato.

1 Resposta

Dieval_Guizelini

Oi,

eu tenho usado esse código com o JFileChooser, talvez te ajude (veja que crio uma thread, e no main eu faço chamada do método init para quando eu precisar do JFileChooser ele já está criado):

package br.ufpr.bioinfo.genbankexplorer.util;

import br.ufpr.bioinfo.genbankexplorer.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class WinUtil {

    private static javax.swing.JFileChooser fc = null;

    public static ImageIcon loadIcon(String nome) {
        String path = "/resource/image/" + nome;
        ImageIcon ii = new ImageIcon(Main.class.getResource(path));
        return ii;
    }

    public static void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(WinUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(WinUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(WinUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(WinUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static synchronized javax.swing.JFileChooser getChooser() {
        if (fc == null) {
            fc = new javax.swing.JFileChooser();
        }
        fc.setFileSelectionMode( JFileChooser.FILES_ONLY );
        return fc;
    }

    public static synchronized void setChooser(javax.swing.JFileChooser f) {
        fc = f;
    }

    public static void init() {
        Thread t = new Thread() {

            public void run() {
                WinUtil.setChooser(new javax.swing.JFileChooser());
            }
        };
        t.start();
    }
}
E os métodos básicos:
String filename = File.separator+"tmp";
JFileChooser fc = new JFileChooser(new File(filename));

// Show open dialog; this method does not return until the dialog is closed
fc.showOpenDialog(frame);
File selFile = fc.getSelectedFile();

// Show save dialog; this method does not return until the dialog is closed
fc.showSaveDialog(frame);
selFile = fc.getSelectedFile();

Para saber mais:
Example Depot
[url]http://www.exampledepot.com/egs/javax.swing.filechooser/CreateDlg.html[/url]
leepoint
[url]http://leepoint.net/notes-java/GUI/containers/20dialogs/30filechooser.html[/url]
Sun tutoriais
[url]http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html[/url]
API
[url]http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFileChooser.html[/url]

fw

Criado 6 de fevereiro de 2010
Ultima resposta 6 de fev. de 2010
Respostas 1
Participantes 2