Evento click ao botão e uso da classe JFileChooser

4 respostas
dellandre

estou com um pequeno problema, estou com um problema para usar o evento click do botão e carregar um JFileChooser,
se alguém puder me ajudar obrigado.

package buscapalavra;

/**
*

  • @author Dellandre
    
    <em>/
    
    import javax.swing.</em>;
    
    import javax.swing.JPanel.*;
    
    import javax.swing.JLabel;
    
    import java.awt.event.ActionEvent;
    
    import java.awt.event.ActionListener;
    
    public class frmBuscaPalavra {
    
    /** Creates a new instance of frmBuscaPalavra */
    
    public void frmBuscaPalavra() {
    
    JLabel label = new JLabel(Diretório:);
    
    JLabel label2 = new JLabel(Palavra:);
    
    JTextField diretorio = new JTextField(20);
    
    JTextField palavra = new JTextField(10);
    
    JButton localizar = new JButton(Buscar);
    
    JButton pesquisar = new JButton (Pesquisar);
    
    JPanel painel = new JPanel();
    
    JFrame frmBusca = new JFrame();
    
    pesquisar.addActionListener(this);
     
     
     /** adicionando objetos ao painel */
     
     painel.add(label);
     painel.add(diretorio);
     painel.add(label2);
     painel.add(palavra);
     frmBusca.add(painel);
     frmBusca.setVisible(true);
    

    }

    public void actionPerformed(ActionEvent e)
    
    {
    
    if(e.getSource() == pesquisar)
    
    {
    
    JFileChooser jFC = new JFileChooser();
    
    jFC.setVisible(true)
    
    }
    
    }
    
    }
    

se puder me mostrar como carregar essa classe na classe main para que o formulário possa aparecer, agradeço desde já.

4 Respostas

malucocelo

Segue um exemplo:

JButton button = new JButton("Escolha um Arquivo"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser fileChooser = new JFileChooser(); int returnValue = fileChooser.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); System.out.println(selectedFile.getName()); } } });

dellandre

vlw kra funcionou sim, muito obrigado

dellandre

Agora outro problema.

Como faço para mandar o diretório gerado pelo JFileChooser para uma JTextField fora do metodo, se alguém puder me ajudar quanto a isso.

ViniGodoy
seuTextField.setText(seuFileChooser.getSelectedFile().getPath());
Criado 12 de março de 2010
Ultima resposta 12 de mar. de 2010
Respostas 4
Participantes 3