Please see the code and tell me what is worng in it, i am not able to check the multiple checkbox at a time
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;
public class SplitNodeTest
{
private JScrollPane getContent()
{
DefaultMutableTreeNoderoot = new DefaultMutableTreeNode("Root");
DefaultMutableTreeNoderoot1 = new DefaultMutableTreeNode(new SplitNode("Node 1", false));
DefaultMutableTreeNoderoot2 = new DefaultMutableTreeNode(new SplitNode("Node 2", false));
root1.add(root2);
root.add(root1);
// root.add(new DefaultMutableTreeNode(new SplitNode("Node 1", false)));
// root.add(new DefaultMutableTreeNode(new SplitNode("Node 2", true)));
JTree tree= new JTree(newDefaultTreeModel(root));
tree.setEditable(true);
tree.setCellRenderer(new SplitNodeRenderer());
tree.setCellEditor(new SplitNodeEditor(tree,root1));
return new JScrollPane(tree);
}
public static void main(
String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new SplitNodeTest().getContent());
f.setSize(360,300);
f.setLocation(200,200);
f.setVisible(true);
}
}
class SplitNodeextends DefaultMutableTreeNode
{
Stringname;
boolean value;
protected boolean isSelected;
public SplitNode(Strings, boolean isSelected)
{
name = s;
this.isSelected =isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
if (children != null) {
Enumeration enum = children.elements();
while (enum.hasMoreElements()) {
CheckNode node = (CheckNode)enum.nextElement();
node.setSelected(isSelected);
}
}
}
public boolean isSelected() {
return isSelected;
}
public String toString()
{
return "SplitNode[value: " + value + ", name:" + name+ "]";
}
}
class SplitNodeRenderer implementsTreeCellRenderer
{
JLabel label;
JCheckBox checkBox;
JTextField textField;
JPanel panel;
public SplitNodeRenderer()
{
label = new JLabel();
checkBox = new JCheckBox();
checkBox.setBackground(UIManager.getColor("Tree.background"));
checkBox.setBorder(null);
textField =newJTextField();
textField.setEditable(false);
textField.setBackground(UIManager.getColor("Tree.background"));
textField.setBorder(null);
panel = new JPanel();
panel.setOpaque(false);
panel.add(checkBox);
panel.add(textField);
}
public Component getTreeCellRendererComponent(JTree tree,Objectvalue,boolean selected, boolean expanded,
boolean leaf,int row,boolean hasFocus)
{
DefaultMutableTreeNode node =(DefaultMutableTreeNode)value;
if(node.getUserObject() instanceof SplitNode)
{
SplitNode splitNode = (SplitNode)node.getUserObject();
checkBox.setSelected(selected);
textField.setText(splitNode.name);
return panel;
}
else
{
label.setText(node.toString());
return label;
}
}
}
class SplitNodeEditor extends AbstractCellEditor implements TreeCellEditor, ActionListener
{
JLabellabel;
JCheckBoxcheckBox;
JTextFieldtextField;
SplitNodesplitNode;
JComponent editedComponent;
JPanelpanel;
final JTree Tree;
Object value1;
public SplitNodeEditor(JTree tree,Object value)
{
label = new JLabel();
checkBox = new JCheckBox();
checkBox.addActionListener(this);
checkBox.setBackground(UIManager.getColor("Tree.background"));
checkBox.setBorder(null);
this.Tree = tree;
value1= value;
checkBox.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e)
{
System.out.println("hello");
int x = e.getX();
int y = e.getY();
int row = Tree.getRowForLocation(x, y);
TreePath path = Tree.getPathForRow(1);
System.out.println(path.getLastPathComponent());
DefaultMutableTreeNodenode1 = (DefaultMutableTreeNode)value1;
//if(node.getUserObject() instanceofSplitNode)
System.out.println(node1);
//TreePath path = tree.getSelectionPath();
if (path != null && node1 instanceof SplitNode) {
SplitNode node = (SplitNode)path.getLastPathComponent();
boolean isSelected = ! (node.isSelected());
System.out.println(isSelected);
node.setSelected(isSelected);
((DefaultTreeModel) Tree.getModel()).nodeChanged(node);
}
}
publicvoid mouseReleased(MouseEvent e)
{
checkBox.setBorder(null);
}
});
textField = new JTextField();
textField.addActionListener(this);
textField.setBackground(UIManager.getColor("Tree.background"));
textField.setBorder(null);
panel = new JPanel();
panel.setOpaque(false);
panel.add(checkBox);
panel.add(textField);
}
public Component getTreeCellEditorComponent(JTreetree,Objectvalue,boolean isSelected, boolean expanded,boolean leaf,int row)
{
DefaultMutableTreeNodenode = (DefaultMutableTreeNode)value;
if(node.getUserObject() instanceofSplitNode)
{
splitNode =(SplitNode)node.getUserObject();
checkBox.setSelected(isSelected);
textField.setText(splitNode.name);
return panel;
}
else
{
label.setText(node.toString());
return label;
}
}
public Object getCellEditorValue()
{
if(editedComponent == textField)
splitNode.name = textField.getText();
else
splitNode.value = checkBox.isSelected();
returnsplitNode;
}
public boolean isCellEditable(EventObject anEvent)
{
if (anEventinstanceof MouseEvent)
{
/*Point p= ((MouseEvent)anEvent).getPoint();
JTree tree =(JTree)anEvent.getSource();
TreePathpath = tree.getPathForLocation(p.x,p.y);
DefaultMutableTreeNodenode =(DefaultMutableTreeNode)path.getLastPathComponent();
int clickCountToStart=(node.getUserObject() instanceof SplitNode) ? 1 : 2;*/
//return((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
}
return true;
}
public void actionPerformed(ActionEvent e)
{
editedComponent =(JComponent)e.getSource();
super.stopCellEditing();
}
}