Real Time File Search

Asked

Viewed 70 times

-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);
    }
}

1 answer

0

of a researcher on how the JFileChooser

Its implementation is quite simple ex:

        JFileChooser fl = new JFileChooser();
        fl.setDialogTitle("Buscar arquivo"); //titulo do jDialog
        fl.setFileSelectionMode(JFileChooser.FILES_ONLY); //aceita somente arquivos (pode ser usado outra constante do JFileChooser)
        int op = fl.showOpenDialog(null); //abre o jDialog
        if (op == JFileChooser.APPROVE_OPTION) { //se foi clicado em abrir
            //obtem o arquivo utilizando por exemplo File f = fl.getSelectedFile();
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.