Pessoal,
criei um aplicativo para ler um txt
e preciso que o usuario possa escolher
o diretorio onde está este txt.
Tem que ser no swing, alguem pode me
ajudar?
Pessoal,
criei um aplicativo para ler um txt
e preciso que o usuario possa escolher
o diretorio onde está este txt.
Tem que ser no swing, alguem pode me
ajudar?
Você pode usar um JFileChooser:
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/components/filechooser.html
Ou, se você quiser fazer algo como o explorer (muito mais complexo):
http://java.sun.com/products/jfc/tsc/articles/treetable1/
http://java.sun.com/products/jfc/tsc/articles/treetable2/index.html
Use um JFileChooser.
Fiz um componentezinho que contém um JTextField e um JFileChooser, você pode usá-lo para inserir em uma tela, como se fosse um componente JPanel. Veja aqui:
/**
* DirectoryPicker.java
*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileSystemView;
/**
* Componente visual para entrada de nomes de diretórios.
*
* <pre>
* +--Title----------------------+
* | [directory name ] [...] |
* +-----------------------------+
* </pre>
*/
public class DirectoryPicker extends JPanel {
public DirectoryPicker() {
super();
initialize();
}
public String getApproveButtonText() {
return approveButtonText;
}
public String getDialogTitle() {
return dialogTitle;
}
public File getDirectory() {
if (directory == null)
directory = new File(".");
return directory;
}
public String getTitle() {
return title;
}
public void initialize() {
setLayout(new BorderLayout());
add(getTxtDirectory(), BorderLayout.CENTER);
add(getBtnBrowse(), BorderLayout.EAST);
border = new TitledBorder("");
setBorder(border);
}
/**
* Se este método não for chamado, o texto que aparece é "Open"
*
* @param approveButtonText
*/
public void setApproveButtonText(String approveButtonText) {
this.approveButtonText = approveButtonText;
}
/**
* Se este método não for chamado, o texto que aparece é "Open"
*
* @param dialogTitle
*/
public void setDialogTitle(String dialogTitle) {
this.dialogTitle = dialogTitle;
}
/**
* Seta o diretório
*
* @param directory
*/
public void setDirectory(File directory) {
this.directory = directory;
txtDirectory.setText(directory.getAbsolutePath());
}
public void setTitle(String title) {
this.title = title;
border.setTitle(title);
this.repaint(); // isto é porque a borda só é redesenhada se isto tudo também for
}
private JButton getBtnBrowse() {
if (btnBrowse == null) {
btnBrowse = new JButton();
btnBrowse.setText("...");
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfc = null;
File defaultFile = null;
if (txtDirectory.getText().trim().length() == 0) {
// Empty
File[] roots = FileSystemView.getFileSystemView().getRoots();
if (roots.length > 0) {
defaultFile = roots[0];
}
} else {
// Something was input, try finding it
try {
defaultFile = new File(txtDirectory.getText().trim()).getCanonicalFile();
} catch (IOException ex) {
}
}
if (defaultFile == null) {
jfc = new JFileChooser();
} else {
jfc = new JFileChooser(defaultFile);
}
if (dialogTitle != null)
jfc.setDialogTitle(dialogTitle);
jfc.setMultiSelectionEnabled(false);
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (jfc.showDialog(DirectoryPicker.this, approveButtonText) == JFileChooser.APPROVE_OPTION) {
directory = jfc.getSelectedFile();
String path = directory.getPath();
txtDirectory.setText(path);
txtDirectory.setToolTipText(path);
}
}
});
}
return btnBrowse;
}
private JTextField getTxtDirectory() {
if (txtDirectory == null) {
txtDirectory = new JTextField();
}
return txtDirectory;
}
private String approveButtonText;
private TitledBorder border;
private JButton btnBrowse;
private String dialogTitle;
/**
* Selected directory
*/
private File directory;
/**
* Title
*/
private String title;
private JTextField txtDirectory;
}
Exemplo de uso.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ExemploUsoDirectoryPicker extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private DirectoryPicker dirPicker = null;
private DirectoryPicker getDirPicker() {
if (dirPicker == null) {
dirPicker = new DirectoryPicker();
}
return dirPicker;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ExemploUsoDirectoryPicker thisClass = new ExemploUsoDirectoryPicker();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public ExemploUsoDirectoryPicker() {
super();
initialize();
}
private void initialize() {
this.setSize(450, 100);
this.setContentPane(getJContentPane());
this.setTitle("Exemplo de Uso - DirectoryPicker");
getDirPicker().setTitle("Diretório de saída");
getDirPicker().setDialogTitle("Escolha o diretório de saída");
getDirPicker().setApproveButtonText("Escolher");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setText("Clique no botão \"...\" para escolher um nome de diretório");
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(jLabel, BorderLayout.NORTH);
jContentPane.add(getDirPicker(), BorderLayout.CENTER);
}
return jContentPane;
}
}
Valeu,
pessoal,
vocês me ajudaram rápido e rasteiro.
vou usar o JFileChooser.
Já fiz um teste e deu certo.
Abraço a todos.