如何在 GUI 中使用 JTree 以升序显示文件名?
How to display file names in ascending order using JTree in GUI?
我在一个文件夹中有一组文件,所有文件都以相似的名称模式开头。这是一个例子:
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();
}
}
}
我面临的问题是,排序方法returns 文件数组和 JTree 使用的是 File 数据类型。我该如何解决?
通过更改 addNodes 方法解决了问题。
我在一个文件夹中有一组文件,所有文件都以相似的名称模式开头。这是一个例子:
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();
}
}
}
我面临的问题是,排序方法returns 文件数组和 JTree 使用的是 File 数据类型。我该如何解决?
通过更改 addNodes 方法解决了问题。