Thanks for the reply. But in this code instead of the textfield i need a label and i should be able to select it whenever it is clicked. Selecting the node should not tick/untick the checkbox. I need to populate the selected node with somme more child nodes, only when the node text is clicked. But if i click the checkbox nither the node should be populated with child nodes nor the corresponding label should not be selected.
Basically the implementation should: checkbox and the node selection work wherever the mouse is clicked.
Here is the modified code.
Please give me some solution soon. I am struggling with this requirement from quite a long time.
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.tree.*;
public class SplitNodeTest {
private JScrollPane getContent() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode(new SplitNode("Root", false, ".//images//folder.gif"));
root.add(new DefaultMutableTreeNode(new SplitNode("Node 1", false, ".//images//folder.gif")));
root.add(new DefaultMutableTreeNode(new SplitNode("Node 2", true, ".//images//folder.gif")));
JTree tree = new JTree(new DefaultTreeModel(root));
tree.setEditable(true);
tree.setCellRenderer(new SplitNodeRenderer());
tree.setCellEditor(new SplitNodeEditor());
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 SplitNode {
String name;
boolean value;
String img;
public SplitNode(String s, boolean b, String img) {
this.name = s;
this.value = b;
this.img = img;
}
public String toString() {
return "SplitNode[value: " + value + ", name:" + name + "]";
}
}
class SplitNodeRenderer implements TreeCellRenderer {
JLabel label;
JLabel imgLabel;
JCheckBox checkBox;
JLabel textField;
JPanel panel;
public String imgname;
public SplitNodeRenderer() {
label = new JLabel();
imgLabel = new JLabel();
checkBox = new JCheckBox();
checkBox.setBackground(UIManager.getColor("Tree.background"));
checkBox.setBorder(null);
textField = new JLabel();
//textField.setEditable(false);
textField.setBackground(UIManager.getColor("Tree.background"));
textField.setBorder(null);
panel = new JPanel();
panel.setOpaque(false);
panel.add(checkBox);
panel.add(imgLabel);
panel.add(textField);
}
public Component getTreeCellRendererComponent(JTree tree,
Object value,
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(splitNode.value);
textField.setText(splitNode.name);
imgname = splitNode.img;
// imgLabel.setIcon(new ImageIcon(".//TreefolderImage//folder.gif"));
return panel;
} else {
label.setText(node.toString());
return label;
}
}
}
class SplitNodeEditor extends AbstractCellEditor
implements TreeCellEditor, ActionListener {
JLabel label;
JLabel imgLabel;
JCheckBox checkBox;
JLabel textField;
SplitNode splitNode;
JComponent editedComponent;
JPanel panel;
String imgname;
public SplitNodeEditor() {
label = new JLabel();
imgLabel = new JLabel();
checkBox = new JCheckBox();
checkBox.addActionListener(this);
checkBox.setBackground(UIManager.getColor("Tree.background"));
checkBox.setBorder(null);
checkBox.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
checkBox.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
}
public void mouseReleased(MouseEvent e) {
checkBox.setBorder(null);
}
});
textField = new JLabel();
//textField.addActionListener(this);
textField.setBackground(UIManager.getColor("Tree.background"));
textField.setBorder(null);
panel = new JPanel();
panel.setOpaque(false);
panel.add(checkBox);
panel.add(imgLabel);
panel.add(textField);
}
public Component getTreeCellEditorComponent(JTree tree,
Object value,
boolean isSelected,
boolean expanded,
boolean leaf,
int row) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
if(node.getUserObject() instanceof SplitNode) {
splitNode = (SplitNode)node.getUserObject();
checkBox.setSelected(splitNode.value);
textField.setText(splitNode.name);
imgname = splitNode.img;
// imgLabel.setIcon(new ImageIcon(".//TreefolderImage//folder.gif"));
return panel;
} else {
label.setText(node.toString());
return label;
}
}
public Object getCellEditorValue() {
if(editedComponent == textField)
splitNode.name = textField.getText();
else
splitNode.value = checkBox.isSelected();
return splitNode;
}
public boolean isCellEditable(EventObject anEvent) {
if (anEvent instanceof MouseEvent) {
Point p = ((MouseEvent)anEvent).getPoint();
JTree tree = (JTree)anEvent.getSource();
TreePath path = tree.getPathForLocation(p.x, p.y);
DefaultMutableTreeNode node = (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();
}
}