Hallo,
Kann mir jemand erklären, wie ich nur den letzten Pfad des Verzeichnisbaumes auslesen kann?
ich hab schon gegoogelt. jedoch ohne erfolg!
als Ausgabe sollte nur:
C:/Desktop/Java/xxx.doc
anstatt
C:/Desktop/Java
C:/Desktop/Java/xxx.doc
erscheinen
danke im voraus
erdmulch
import java.io.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
import javax.swing.tree.*;
public class FileTreeTest extends JTree
{
public static void main( String[] args )
{
JFrame frame = new JFrame("JTree-Test");
frame.getContentPane().add( new JScrollPane( new FileTreeTest() ) );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
private static FileSystemView SYSTEM = FileSystemView.getFileSystemView();
public FileTreeTest()
{
setModel( new FileTreeModel() );
setCellRenderer( new FileTreeCellRenderer() );
}
private class FileTreeCellRenderer extends DefaultTreeCellRenderer
{
public FileTreeCellRenderer()
{
}
public Component getTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean exp, boolean leaf, int row, boolean focus ){
super.getTreeCellRendererComponent( tree, value, sel, exp, leaf, row, focus );
File file = (File)value;
setIcon( SYSTEM.getSystemIcon( file ) );
setText( SYSTEM.getSystemDisplayName( file ));
return this;
}
}
private class FileTreeModel implements TreeModel
{
public FileTreeModel()
{
}
public Object getRoot()
{
return SYSTEM.getHomeDirectory(); // <<<<<<<<<<<<<<<<<<<<<<< Der ist wichtig
}
public int getChildCount(Object parent)
{
return SYSTEM.getFiles( (File) parent, true).length;
}
public boolean isLeaf(Object node)
{
System.out.println(node);
return ( (File) node).isFile();
}
public void addTreeModelListener(TreeModelListener l)
{
}
public void removeTreeModelListener(TreeModelListener l)
{
}
public Object getChild(Object parent, int index)
{
return SYSTEM.getFiles( (File) parent, true)[ index];
}
public int getIndexOfChild(Object parent, Object child)
{
File[] files = SYSTEM.getFiles( (File) parent, true);
for (int i = 0; i < files.length; i++)
if (files == child)
return i;
return -1;
}
public void valueForPathChanged(TreePath path, Object newValue)
{
}
}
}
Kann mir jemand erklären, wie ich nur den letzten Pfad des Verzeichnisbaumes auslesen kann?
ich hab schon gegoogelt. jedoch ohne erfolg!
als Ausgabe sollte nur:
C:/Desktop/Java/xxx.doc
anstatt
C:/Desktop/Java
C:/Desktop/Java/xxx.doc
erscheinen
danke im voraus
erdmulch
import java.io.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
import javax.swing.tree.*;
public class FileTreeTest extends JTree
{
public static void main( String[] args )
{
JFrame frame = new JFrame("JTree-Test");
frame.getContentPane().add( new JScrollPane( new FileTreeTest() ) );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
private static FileSystemView SYSTEM = FileSystemView.getFileSystemView();
public FileTreeTest()
{
setModel( new FileTreeModel() );
setCellRenderer( new FileTreeCellRenderer() );
}
private class FileTreeCellRenderer extends DefaultTreeCellRenderer
{
public FileTreeCellRenderer()
{
}
public Component getTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean exp, boolean leaf, int row, boolean focus ){
super.getTreeCellRendererComponent( tree, value, sel, exp, leaf, row, focus );
File file = (File)value;
setIcon( SYSTEM.getSystemIcon( file ) );
setText( SYSTEM.getSystemDisplayName( file ));
return this;
}
}
private class FileTreeModel implements TreeModel
{
public FileTreeModel()
{
}
public Object getRoot()
{
return SYSTEM.getHomeDirectory(); // <<<<<<<<<<<<<<<<<<<<<<< Der ist wichtig
}
public int getChildCount(Object parent)
{
return SYSTEM.getFiles( (File) parent, true).length;
}
public boolean isLeaf(Object node)
{
System.out.println(node);
return ( (File) node).isFile();
}
public void addTreeModelListener(TreeModelListener l)
{
}
public void removeTreeModelListener(TreeModelListener l)
{
}
public Object getChild(Object parent, int index)
{
return SYSTEM.getFiles( (File) parent, true)[ index];
}
public int getIndexOfChild(Object parent, Object child)
{
File[] files = SYSTEM.getFiles( (File) parent, true);
for (int i = 0; i < files.length; i++)
if (files == child)
return i;
return -1;
}
public void valueForPathChanged(TreePath path, Object newValue)
{
}
}
}