DirectoryChooser
FileTree
<code=java> package tests.swing;
import java.io.File; import java.io.FileFilter; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List;
import javax.swing.JTree; import javax.swing.event.TreeExpansionEvent; import javax.swing.event.TreeWillExpandListener; import javax.swing.filechooser.FileSystemView; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel;
public class FileTree extends JTree implements TreeWillExpandListener {
private static class FileComparator implements Comparator<File> { public int compare(File f1, File f2) { if(f1.isDirectory() ^ f2.isDirectory()) { return (f1.isDirectory() ? -1 : 1); } return f1.getName().compareToIgnoreCase(f2.getName()); } }
private static class DirectoryFilter implements FileFilter { public boolean accept(File f) { return f.isDirectory(); } }
private static class AcceptAllFilter implements FileFilter { public boolean accept(File f) { return true; } }
private static final Comparator<File> FILE_COMPARATOR = new FileComparator();
/** Ein FileFilter, der nur Verzeichnisse akzeptiert */ public static final FileFilter DIRECTORY_FILTER = new DirectoryFilter();
/** Ein FileFilter der alle Typen von Dateien und Verzeichnisse akzeptiert */ public static final FileFilter ACCEPT_ALL_FILTER = new AcceptAllFilter();
/** Der aktuelle FileFilter */ protected FileFilter filter = null;
public FileTree() { // Home Verzeichnis feststellen File homeDir = FileSystemView.getFileSystemView().getHomeDirectory();
// Wurzelelement erstellen DefaultMutableTreeNode root = new DefaultMutableTreeNode(homeDir);
// Model erstellen mit root als Wurzelelement DefaultTreeModel model = new DefaultTreeModel(root); setModel(model); setShowsRootHandles(true);
// TreeCellRenderer setzen. setCellRenderer(new FileTreeRenderer());
// Wurzel aufklappen expandPath(root);
// Listener hinzufügen addTreeWillExpandListener(this);
// und so ... getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); }
/** * Erweitert einen Pfad im Tree. * * @param d Den Parent Knoten */ private void expandPath(final DefaultMutableTreeNode d) { d.removeAllChildren(); File[]; tempf = ((File) d.getUserObject()).listFiles();
List<File> files = Arrays.asList(tempf); Collections.sort(files, FILE_COMPARATOR);
DefaultMutableTreeNode temp = null;
for(File file: files) { if(filter != null && !filter.accept(file)) continue;
temp = new DefaultMutableTreeNode(file); if(file.isDirectory()) temp.add(new DefaultMutableTreeNode(null)); d.add(temp); } ((DefaultTreeModel) getModel()).reload(d); }
/** * Setzt den FileFilter und setzt den Tree zurück. * * @param f Der neue FileFilter */ public void setFileFilter(FileFilter f) { filter = f; expandPath((DefaultMutableTreeNode) getModel().getRoot()); }
/** * Liefert den aktuell gesetzten FileFilter zurück * @return Den FileFilter */ public FileFilter getFileFilter() { return filter; }
public File getSelectedFile() { TreePath selectionPath = getSelectionPath();
if(selectionPath == null) return null;
DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath.getLastPathComponent(); return (File) node.getUserObject(); }
public void treeWillCollapse(TreeExpansionEvent e) { ((DefaultMutableTreeNode) (e.getPath().getLastPathComponent())).removeAllChildren(); ((DefaultMutableTreeNode) (e.getPath().getLastPathComponent())).add(new DefaultMutableTreeNode(null)); }
public void treeWillExpand(TreeExpansionEvent e) { expandPath((DefaultMutableTreeNode) (e.getPath().getLastPathComponent())); }
} </code=java>
FileTreeRenderer
<code=java> package tests.swing;
import java.awt.Component; import java.io.File;
import javax.swing.JTree; import javax.swing.filechooser.FileSystemView; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer;
class FileTreeRenderer extends DefaultTreeCellRenderer {
private FileSystemView fsv = FileSystemView.getFileSystemView();
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); Object user = ((DefaultMutableTreeNode) value).getUserObject();
if(user instanceof File) { File f = (File) user; String name = f.getName(); setIcon(fsv.getSystemIcon(f)); setText(fsv.getSystemDisplayName(f)); }
return this; }
} </code=java>
FileTreeTest
<code=java> package tests.swing;
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.io.File;
import javax.swing.AbstractAction; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.UIManager; import javax.swing.border.Border;
public class FileTreeTest {
/** * @param args */ public static void main(String[] args) throws Exception { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() ); final JFrame f = new JFrame("Datei auswählen"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final FileTree chooser = new FileTree(); chooser.setFileFilter(FileTree.DIRECTORY_FILTER); JScrollPane scrp = new JScrollPane(chooser); Border empty = BorderFactory.createEmptyBorder(5, 5, 2, 5); scrp.setBorder(BorderFactory.createCompoundBorder(empty, scrp.getBorder()));
JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT)); bottom.add(new JButton(new AbstractAction("OK") {
public void actionPerformed(ActionEvent e) { File file = chooser.getSelectedFile(); String s = "'" + file+ "'"; if(file == null) s = "nix";
JOptionPane.showMessageDialog(f, "Es wurde "+s+" selektiert."); f.dispose(); } }));
f.add(scrp); f.add(bottom, BorderLayout.SOUTH);
f.setSize(300, 400); f.setLocationRelativeTo(null); f.setVisible(true); }
} </code=Java> --Benutzer:Roar (Janni Kovacs - 03.12.2005, 21:23)