如何在GUI中使用JTree以升序显示文件名?

时间:2015-08-05 18:35:14

标签: file sorting arraylist jtree

我在一个文件夹中有一组文件,所有这些文件都以类似的名称模式开头。这是一个例子:

Spectrum_1.txt
Spectrum_2.txt
Spectrum_3.txt
....
....
....
Spectrum_10.txt
Spectrum_11.txt
....
....
....
Spectrum_20.txt
....
....
....

我能够列出指定文件夹中的所有文件,但列表不是频谱号的升序。我有一种排序方法可以解决这个问题:

public class SortFile {

    /**
     * Method which takes list of files as a fileArray and sorts them 
     * 
     * @param File[] files
     * @return File[] files
     */ 

    public File[] sortByNumber(File[] files) {

        Arrays.sort(files, new Comparator<File>() {
            //@Override
            public int compare(File o1, File o2) {
                int n1 = extractNumber(o1.getName());
                int n2 = extractNumber(o2.getName());
                return n1 - n2;
            }

            private int extractNumber(String name) {
                int i = 0;
                try {
                    int s = name.indexOf('_')+1;
                    int e = name.lastIndexOf('.');
                    String number = name.substring(s, e);
                    i = Integer.parseInt(number);
                } catch(Exception e) {
                    i = 0; // if filename does not match the format
                    // then default to 0
                }
                return i;
            }
        });

        return files;

    }
}

但是,我不知道如何使用JTree代码:

public void actionPerformed(ActionEvent e) {

        //Handle open button action.
        if (e.getSource() == OpenFileButton) {
            int returnVal = fc.showOpenDialog(GUIMain.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile();
                File[] filesInDirectory = file.listFiles();

                SortFile sf = new SortFile();

                // Calls sortByNumber method in class SortFile to list the files number wise
                filesInDirectory = sf.sortByNumber(filesInDirectory);

// Jtree takes the File datatype as input
       tree = new JTree(addNodes(null, file));

                // Add a listener
                tree.addTreeSelectionListener(new TreeSelectionListener() {
                    public void valueChanged(TreeSelectionEvent e) {
                        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e
                                .getPath().getLastPathComponent();
                        System.out.println("You selected " + node);
                    }
                });

                spectralFilesScrollPane = new JScrollPane();
                spectralFilesScrollPane.getViewport().add(tree);
                spectralFilesScrollPane.setPreferredSize(new Dimension(290, 465));
                content.add(spectralFilesScrollPane);
//               // content.invalidate();
                content.validate();
                content.repaint();

            }
        }
    }

我面临的问题是,sort方法返回一个文件数组,而JTree正在使用File数据类型。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

通过更改addNodes方法解决了这个问题。