Hi All,
I have the requirement like this.
I have to fetch syatem files and have to choose files and folders accordingly.
I have made a program which fetches the system files.
I have to put checkboxes beside each and every files and folders.
I am giving the code ..Please see any body can help..
Any help will be greatly appreciated..
Thanks a lot in advance..
----------------------------
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.Dimension;//
import java.awt.*;
import java.io.*;
public class FileExplorer {
public static void main(
String[] argv ) {
JFrame frame = new JFrame( "File Explorer" );
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit( 0 );
}
});
System.out.println("Inside main()..");
FileSystemModel model = new FileSystemModel();
FileSystemTreePanel fileTree = new FileSystemTreePanel( model );
JScrollPane treeScroller = new JScrollPane( fileTree );
//JScrollPane tableScroller = JTable.createScrollPaneForTable( table );
treeScroller.setMinimumSize( new Dimension( 0, 0 ) );
frame.getContentPane().add(treeScroller );
//frame.getContentPane().add(tree );
frame.setSize( 400, 400 );
frame.setResizable(false);
//frame.pack();
frame.show();
}
--------------------------------
import javax.swing.*;
import javax.swing.tree.*;
import java.io.*;
public class FileSystemModel extends AbstractTreeModel implements Serializable {
//public class FileSystemModel extends DefaultMutableTreeNode implements Serializable {
String root;
public FileSystemModel() {
this( System.getProperty( "user.home" ) );
System.out.println("Gettng the root path from FSM");
}
public FileSystemModel( String startPath ) {
System.out.println("Inside FSM constructure with one arg..");
root = startPath;
}
public Object getRoot() {
return new File( root );
}
public Object getChild( Object parent, int index ) {
File directory = (File)parent;
String[] children = directory.list();
return new File( directory, children[index] );
}
public int getChildCount( Object parent ) {
File fileSysEntity = (File)parent;
if ( fileSysEntity.isDirectory() ) {
String[] children = fileSysEntity.list();
return children.length;
}
else {
return 0;
}
}
public boolean isLeaf( Object node ) {
return ((File)node).isFile();
}
public void valueForPathChanged( TreePath path, Object newValue ) {
}
public int getIndexOfChild( Object parent, Object child ) {
File directory = (File)parent;
File fileSysEntity = (File)child;
String[] children = directory.list();
int result = -1;
for ( int i = 0; i < children.length; ++i ) {
if ( fileSysEntity.getName().equals( children[i] ) ) {
result = i;
break;
}
}
return result;
}
}
---------------------------------
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class FileSystemTreePanel extends JPanel {
private JTree tree;
public FileSystemTreePanel() {
this( new FileSystemModel() );
}
public FileSystemTreePanel( String startPath ) {
this( new FileSystemModel( startPath ) );
}
public FileSystemTreePanel( FileSystemModel model ) {
System.out.println("FSTP:Getting the tree");
tree = new JTree( model )
{
public String convertValueToText(Object value, boolean selected,
boolean expanded, boolean leaf, int row,
boolean hasFocus) {
System.out.println("FSTP:inside convertValueToText()..");
return ((File)value).getName();
}
};
tree.setRootVisible( false );
tree.setShowsRootHandles( true );
tree.putClientProperty( "JTree.lineStyle", "Angled" );
//tree.putClientProperty( "JTree.EmptySelectionModel.lineStyle", "Angled" );
setLayout( new BorderLayout() );
add( tree, BorderLayout.CENTER );
}
public JTree getTree() {
//public JTree.EmptySelectionModel getTree() {
return tree;
}
}