-1
I’m trying to make a program that provides real-time file search feature.
As the user type the name of a directory in the text box, the program should search and display in the text area the names of all files and subdirectories found in the directory, whose name is in the text box.
I managed to get him to list the folders in the directory C:/
, but the subfolders and files it does not list.
Follows the code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileFilter;
public class PesquisarArquivoGUI extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea = new JTextArea();
public PesquisarArquivoGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 410, 423);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblDiretrio = new JLabel("Diret\u00F3rio:");
lblDiretrio.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblDiretrio.setBounds(10, 11, 64, 17);
contentPane.add(lblDiretrio);
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
File arquivo = new File("C:\\");
File[] lista = arquivo.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().contains(textField.getText());
}
});
for(File f : lista)
textArea.setText(f.getPath());
}
});
textField.setBounds(73, 11, 311, 20);
contentPane.add(textField);
textField.setColumns(10);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBounds(0, 43, 394, 341);
contentPane.add(scrollPane);
setVisible(true);
}
}