This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Apply CheckBox in Each Node in a JTree which fetches system file(JCheckbox and Jtree)  RSS feed

 
Piyush Roy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you want to render each node as a checkbox, use something like this

 
Piyush Roy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maneesh..
Now checkboxes are coming..
Still my prob is not solved ..I am not able to select multiple files or folders from the tree .
i cant able to check or uncheck the checkboxes.
Please guide me..
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Piyush Roy:

Still my prob is not solved ..I am not able to select multiple files or folders from the tree .
i cant able to check or uncheck the checkboxes.
Please guide me..


If I understand you correctly this is what you want to do.
Render a JTree for the file system
Folders should be rendered as node
Subfolder should be also rendererd as nodes
Files should be rendered as leaves
If you select a folder, all the subsequent nodes (folders as well as files) should also get selected.

What you need is a check box tree.
Check out this page

Best of luck.
 
shweta raghuwanshi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Guys ,

Even same kind of problem I am facing, only diffrence is I need checkbox only with root nodes not with child elements . Please refer the link and help me for the same

http://www.coderanch.com/t/534210/GUI/java/Show-selected-file-folder-tree

Regards,
Shweta
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!