Runtime checkbox with Jtree
Hi friends,
i am happy to join in this forum. i have a problem in adding checkbox in runtime.
in my code i can create Jtree(c:). In this all c: files and folders i want to add checkbox. could anyone help me please this is urgent for me.
Below i attach code (i want to add only checkbox)
[code}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import java.io.*;
import java.util.Vector;
public class MyTree extends JFrame implements ActionListener{
File[] rt;
File drive;
private DefaultComboBoxModel comboModel;
MyTreeModel model;
File root = null;
JTree tree;
public MyTree() {
initComponents();
}
public void initComponents() {
setLayout(new BorderLayout());
setDefaultCloseOperation(javax.swing.WindowConstan ts.DISPOSE_ON_CLOSE);
panelB = new javax.swing.JPanel();
//panelB.setLayout(new BorderLayout());
//panelB.setLayout(null);
comboModel = new DefaultComboBoxModel();
selectDrive = new JComboBox(comboModel);
rt=File.listRoots();
for (int i=0; i<rt.length; i++)
{
drive = rt[i];
//comboModel.addElement(drive);
comboModel.addElement(drive);
}
selectDrive.setModel(comboModel);
selectDrive.addActionListener(this);
selectDrive.setActionCommand("sel_drive");
tree = new JTree();
tscrollPane = new JScrollPane(tree);
displayTree();
//panelB.add(selectDrive,BorderLayout.NORTH);
// panelB.add(tscrollPane,BorderLayout.CENTER);
panelB.add(selectDrive,BorderLayout.NORTH);
panelB.add(tscrollPane,BorderLayout.CENTER);
add(panelB,BorderLayout.NORTH);
pack();
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if(cmd.equals("sel_drive")){
//displayTree();
}
}
public void displayTree(){
Object obj = comboModel.getSelectedItem();
String str = obj.toString();
File f = new File(str);
System.out.println();
model = new MyTreeModel(f);
tree.setModel(model);
}
public static void main(String args[]){
MyTree treefr = new MyTree();
treefr.setSize(500, 500);
treefr.setVisible(true);
}
public JComboBox selectDrive;
public JPanel panelB;
public JScrollPane tscrollPane;
}
[/code]
|