'How to output ArrayList in JFrame interface in Java

I'm a complete beginner to programming and this is my first question. I want to create a program in java to search images' paths on windows OS. All the paths should appear as a list in the interface I programmed. my interface

How can I output all the paths? I tried to use this:

list.setText(file.getAbsolutePath());

but .setText deletes all previous strings and leaves only the last.

Please help and thank you

My code:

public class Main {

public static void main(String[] args) {

    JFrame window = new JFrame("Search Window");
    window.setBounds(5,5, 500, 500);
    window.setLayout(null);

    JButton button = new JButton("Search Images");
    button.setBounds(160, 20, 150, 50);
    button.setBackground(Color.gray);
    button.setForeground(Color.white);
    window.add(button);

    JLabel label = new JLabel("Output Here");
    label.setBounds(20, 80, 150, 50);
    window.add(label);

    JList list = new JList();
    list.setBounds(20, 130, 440, 30);
    window.add(list);

    ActionListener actionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            ArrayList<File> fileList = new ArrayList<>();
            searchFiles(new File("D:\\"), fileList);
            for (File file: fileList) {

                System.out.println(file.getAbsolutePath());
            }

        }

        private static void searchFiles(File rootFile, List<File> fileList) {
            if (rootFile.isDirectory()) {
                File[] directoryFiles = rootFile.listFiles();
                if (directoryFiles != null) {
                    for(File file: directoryFiles) {
                        if (file.isDirectory()) {
                            searchFiles(file, fileList);
                        } else {
                            if (file.getName().toLowerCase().endsWith(".jpg")) {
                                fileList.add(file);
                            }
                        }

                    }
                }

            }

        }

        };
        button.addActionListener(actionListener);

        window.setVisible(true);

    }

}



Solution 1:[1]

As said in comment, you can modify the section into something like this:

String temp = list.getText() + file.getAbsolutePath();
list.setText(temp);

which you can then also append , or other syntax if needed.

Below are the codes:

public class Main {

    public static void main(String[] args) {

        JFrame window = new JFrame("Search Window");
        window.setBounds(5,5, 500, 500);
        window.setLayout(null);

        JButton button = new JButton("Search Images");
        button.setBounds(160, 20, 150, 50);
        button.setBackground(Color.gray);
        button.setForeground(Color.white);
        window.add(button);

        JLabel label = new JLabel("Output Here");
        label.setBounds(20, 80, 150, 50);
        window.add(label);

        JList list = new JList();
        list.setBounds(20, 130, 440, 30);
        window.add(list);

        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                ArrayList<File> fileList = new ArrayList<>();
                searchFiles(new File("D:\\"), fileList);
                for (File file: fileList) {
                    
                    // Assume you need it here.
                    String temp = list.getText() + file.getAbsolutePath();
                    list.setText(temp);
                }

            }

            private static void searchFiles(File rootFile, List<File> fileList) {
                if (rootFile.isDirectory()) {
                    File[] directoryFiles = rootFile.listFiles();
                    if (directoryFiles != null) {
                        for(File file: directoryFiles) {
                            if (file.isDirectory()) {
                                searchFiles(file, fileList);
                            } else {
                                if (file.getName().toLowerCase().endsWith(".jpg")) {
                                    fileList.add(file);
                                }
                            }

                        }
                    }

                }

            }

        };
        button.addActionListener(actionListener);
    }

Hope this answer helps you well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hokwanhung