0

So, I have classes

public class Walker implements Runnable{
    public File rootDirectory;
    private ScrollPane outputField;

    Walker(String rootDirectoryPath,ScrollPane outputField ) throws FileNotFoundException{
        this.outputField = outputField;
        File file = new File(rootDirectoryPath);
        if (file.exists() && file.isDirectory())
            rootDirectory = file;
        else
            throw new FileNotFoundException();
    }


    @Override
    public void run(){
        scanDirectory(rootDirectory);
    }


    void scanDirectory(File directory){
        File[] files = directory.listFiles();
        if (files != null){
            for (File f : files){
                final String path = f.getAbsolutePath();

                if (SwingUtilities.isEventDispatchThread()){
                    outputField.append(path);
                } else {
                    SwingUtilities.invokeLater(new Runnable(){
                        @Override
                        public void run(){
                            outputField.append(path);
                        }
                    });
                }

                if (f.isDirectory() && !f.isHidden()){
                    scanDirectory(f);
                }
            }
        }
    }
}

and class ScrollPane

import javax.swing.*;

class ScrollPane extends JScrollPane{
    private static JTextArea jta = new JTextArea();
    private final String WARNING_MESSAGE =
            "Вы ввели неверный путь или он ссылается на регулярный файл.";

    ScrollPane(){
        super(jta);
        setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    }

    void append(String s){
        jta.append(s + '\n');
        getVerticalScrollBar().setValue(getVerticalScrollBar().getMaximum());
    }

    /**
     * Метод очищает панель вывода.
     */
    void clearAll(){
        jta.setText("");
    }

    void showWarningMessage(){
        jta.append(WARNING_MESSAGE);
    }
}

I'd like to make a tree folders with file like this: enter image description here

I want to replace so. so that a folder / file tree is created in it For example, the FileSystemView class has a getRoots () method that returns directories, and getFiles (), which returns files. But I can not apply it to my task, because I need to create a tree not of the entire file system. but only starting from the specified directory. That is, the user specifies a directory, for example D: \ JAVA \ and on the exit should get the JAVA tree / and further folders and files. which include

Sergei
  • 1
  • 4

2 Answers2

0

I think you can display the tree using JTree and change the icons, even dynamically.

Jaime
  • 5,435
  • 2
  • 18
  • 21
0

I need to create a tree not of the entire file system. but only starting from the specified directory.

Use the File class. You can use the listFiles(...) method to get the files/directories of the specified File.

camickr
  • 321,443
  • 19
  • 166
  • 288