Alguem sabe como pegar um caminho completo de um arquivo pelo JFIleChooser, quando eu escolho um arquivo ele me retorna somente o nome do arquivo mas eu preciso do diretório completo alguem poderia me ajudar???
[color=darkblue]Bem, não sei se ajuda, fiz o código agora, está meio porco:[/color]
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class JPanelTeste extends Panel {
JTextField txtWay = new JTextField(10);;
JButton importar = new JButton("TESTE");
/**
* Método construtor
*
*/
public JPanelTeste(){
importar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser(System
.getProperty("user.dir"));
fc.setMultiSelectionEnabled(false);
javax.swing.filechooser.FileFilter filter = new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
return f.isDirectory()
|| f.getName().toLowerCase().endsWith(
".txt");
}
public String getDescription() {
return "(*.txt)";
// return "(*.xls)";
}
};
fc.addChoosableFileFilter(filter);
int returnVal = fc.showDialog(null,
"Open");
if (returnVal == JFileChooser.APPROVE_OPTION) {
txtWay.setText(fc.getSelectedFile().getAbsolutePath());
} else if (returnVal == JFileChooser.CANCEL_OPTION) {
return;
} else if (returnVal == JFileChooser.UNDEFINED_CONDITION) {
return;
}
}
});
JPanel panel = new JPanel();
txtWay.setEditable(true);
panel.add(txtWay);
panel.add(importar);
JFrame frame = new JFrame();
frame.getContentPane().add(panel);
frame.setSize(new Dimension(300, 300));
frame.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
JPanelTeste testePanel = new JPanelTeste();
testePanel.show();
// TODO Auto-generated method stub
}
}
Valeu ajudou e muito!!!