JavaStarter
Grünschnabel
Hab mal eine Lösung zusammengestellt, wo man zwei Sortierverfahren auswählen kann:
Gruß, Manuel
Code:
/**
*
*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeMap;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
/**
* @author Manuel Beck
*
*/
public class JTreeDirectoryView extends JFrame {
File root;
JTree tree;
Comparator currentComparator = comparator2;
final static Comparator comparator = new Comparator() {
public int compare(Object o1, Object o2) {
File file1 = (File)o1;
File file2 = (File)o2;
if (file1.isDirectory() && file2.isDirectory()){
return file1.getName().compareTo(file2.getName());
} else if (file1.isDirectory()){
return -1;
}
else if (file2.isDirectory()) {
return +1;
}
int point1 = file1.getName().lastIndexOf(".");
int point2 = file2.getName().lastIndexOf(".");
if (point1 == -1 || point2 == -1) return -1;
String fileEnd1 = file1.getName().substring(point1+1, file1.getName().length()-1).toLowerCase();
String fileEnd2 = file2.getName().substring(point2+1, file2.getName().length()-1).toLowerCase();
int result = fileEnd1.compareTo(fileEnd2);
if (result == 0) return file1.getName().compareTo(file2.getName());
return result;
}
};
final static Comparator comparator2 = new Comparator() {
public int compare(Object o1, Object o2) {
File file1 = (File)o1;
File file2 = (File)o2;
return file1.getName().compareTo(file2.getName());
}
};
public JTreeDirectoryView() {
super("JTreeDirectoryView");
setDefaultCloseOperation(EXIT_ON_CLOSE);
tree = new JTree();
buildTreeModelAccordingToDirectoryStructure(root = new File("C:/"),
tree);
getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
getContentPane().add(new JButton(new AbstractAction("Nach DateiTyp sortieren") {
public void actionPerformed(ActionEvent e) {
currentComparator = comparator;
tree.updateUI();
}
}),BorderLayout.SOUTH);
getContentPane().add(new JButton(new AbstractAction("Nach Name sortieren") {
public void actionPerformed(ActionEvent e) {
currentComparator = comparator2;
tree.updateUI();
}
}),BorderLayout.NORTH);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
FileSystemTreeNode treeNode = (FileSystemTreeNode)e.getNewLeadSelectionPath().getLastPathComponent();
File f = (File)treeNode.getUserObject();
System.out.println("Gewählte Datei: " + f.getName());
System.out.println("Ist ein Ordner: " + f.isDirectory());
}
});
pack();
setVisible(true);
}
void buildTreeModelAccordingToDirectoryStructure(File file, JTree tree) {
tree.setModel(new DefaultTreeModel(new FileSystemTreeNode(file)));
}
/**
* @param args
*/
public static void main(String[] args) {
new JTreeDirectoryView();
}
class FileSystemTreeNode extends DefaultMutableTreeNode {
ArrayList<File> currentFiles;
public FileSystemTreeNode(File file) {
setUserObject(file);
}
public int getChildCount() {
filterDirectories((File) getUserObject());
if (currentFiles == null) {
return 0;
}
return currentFiles.size();
}
public TreeNode getChildAt(int index) {
filterDirectories((File) getUserObject());
return new FileSystemTreeNode(currentFiles.get(index));
}
public boolean isLeaf() {
if (((File)getUserObject()).isFile()) return true;
return false;
}
public String toString() {
String string = ((File) getUserObject()).getName();
if (getUserObject() == root) {
string = ((File) getUserObject()).getAbsolutePath();
}
return string;
}
private void filterDirectories(File file) {
TreeMap directoryList = new TreeMap(currentComparator);
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
directoryList.put(files[i], files[i]);
}
currentFiles = new ArrayList<File>();
for(Object o: directoryList.values()) {
File f = (File)o;
currentFiles.add(f);
}
} else {
currentFiles = new ArrayList<File>();
currentFiles.add(file);
}
}
}
}
Gruß, Manuel
Zuletzt bearbeitet: