Hi all,
I'm new to this forum, and I hope we'll cooperate, always.
I'm using a custom cell renderer for jtrees that renders leaf nodes to checkboxes. This was obtained, yet once rendering is OK, I cannot select more than one item of the tree. Thus, rendering is hindering the properties of the default selection model of the tree.
Any help to apply the selection properties with rendering?
Below is my code:
[I]import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.ChangeEvent;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeSelectionModel;
public class CheckBoxNodeTreeSample implements TreeWillExpandListener {
CheckBoxNode accessNode, browseNode, dummyNode, rootNode;
JTree tree;
CheckBoxNode accessibilityOptions[] = {
new CheckBoxNode("Access 1"),
new CheckBoxNode("Access 2")
};
CheckBoxNode browsingOptions[] = { new CheckBoxNode("Browse 1"),
new CheckBoxNode("Browse 2"),
new CheckBoxNode("Browse 3"),
new CheckBoxNode("Browse 4")
};
public static void main(String args[]) {
new CheckBoxNodeTreeSample();
}
public CheckBoxNodeTreeSample(){
final JFrame frame = new JFrame("CheckBox Tree");
browseNode = new CheckBoxNode("Browsing");
accessNode = new CheckBoxNode("Accessibility");
rootNode = new CheckBoxNode("Root");
dummyNode = new CheckBoxNode("Dummy Node");
createInitialTree();
tree = new JTree(rootNode);
CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
tree.setCellRenderer(renderer);
tree.setCellEditor(new CheckBoxNodeEditor(tree));
tree.setEditable(true);
tree.addTreeWillExpandListener(this);
tree.setShowsRootHandles(true);
tree.getSelectionModel().setSelectionMode(TreeSele ctionModel.DISCONTIGUOUS_TREE_SELECTION); //.SINGLE_TREE_SELECTION);
tree.getSelectionModel().addTreeSelectionListener( new TreeSelectionListener(){
public void valueChanged(TreeSelectionEvent se) {
if (tree.getSelectionRows()!=null)
printIt(tree.getSelectionRows());
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if(selectedNode!=null){
String selectedNodeName = selectedNode.toString();
System.out.println(selectedNodeName);
}
}
public void printIt (int a[]){
System.out.print("Selected Nodes: ");
for(int counter=0; counter<a.length; counter++)
System.out.print(a[counter]+" ");
}
});
tree.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent arg0) {
if (arg0.getX()>frame.getWidth()-150 && arg0.getY()>frame.getHeight()-150){
System.out.println(arg0.getX());
System.out.println(arg0.getY());
}
}
});
JScrollPane scrollPane = new JScrollPane(tree);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setBounds(500, 300, 500, 300);
frame.setVisible(true);
}
public void createInitialTree(){
rootNode.add(browseNode);
rootNode.add(accessNode);
browseNode.add(dummyNode);
dummyNode = new CheckBoxNode("Dummy Node");
accessNode.add(dummyNode);
}
public void treeWillCollapse(TreeExpansionEvent arg0) throws ExpandVetoException {
if(arg0.getPath().toString().endsWith(accessNode.t oString()+"]")){
System.out.println("Accessibility Collapsed.");
accessNode.removeAllChildren();
dummyNode = new CheckBoxNode("Dummy Node");
accessNode.add(dummyNode);
}
else if(arg0.getPath().toString().endsWith(browseNode.t oString()+"]")){
System.out.println("Browsing Collapsed.");
browseNode.removeAllChildren();
dummyNode = new CheckBoxNode("Dummy Node");
browseNode.add(dummyNode);
}
}
public void treeWillExpand(TreeExpansionEvent arg0) throws ExpandVetoException {
if(arg0.getPath().toString().endsWith(accessNode.t oString()+"]")){
System.out.println("Accessibility Expanded.");
accessNode.removeAllChildren();
for (int i=0; i<accessibilityOptions.length; i++)
accessNode.add(accessibilityOptions[i]);
}
else if(arg0.getPath().toString().endsWith(browseNode.t oString()+"]")){
System.out.println("Browsing Expanded.");
browseNode.removeAllChildren();
for (int i=0; i<browsingOptions.length; i++)
browseNode.add(browsingOptions);
}
}
}
class CheckBoxNodeRenderer implements TreeCellRenderer {
private JCheckBox leafRenderer = new JCheckBox();
private DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();
protected JCheckBox getLeafRenderer() {
return leafRenderer;
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
Component returnValue;
CheckBoxNode node=null;
if (leaf) {
leafRenderer.setText(value+"");
leafRenderer.setEnabled(tree.isEnabled());
leafRenderer.setSelected(false);
if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
if (userObject instanceof CheckBoxNode) {
node = (CheckBoxNode) userObject;
leafRenderer.setText(node.getText());
leafRenderer.setSelected(node.isSelected());
//System.out.println("Entered Here: "+node+" isSelected: "+node.isSelected());
}
}
returnValue = leafRenderer;
} else {
returnValue = nonLeafRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
}
returnValue.setBackground(null);
return returnValue;
}
}
class CheckBoxNodeEditor extends AbstractCellEditor implements TreeCellEditor {
CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
ChangeEvent changeEvent = null;
JTree tree;
public CheckBoxNodeEditor(JTree tree) {
this.tree = tree;
}
public Object getCellEditorValue() {
JCheckBox checkbox = renderer.getLeafRenderer();
CheckBoxNode checkBoxNode = new CheckBoxNode(checkbox.getText(), checkbox.isSelected());
return checkBoxNode;
}
public boolean isCellEditable(EventObject event) {
return true;
}
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row){
Component editor = renderer.getTreeCellRendererComponent(tree, value, true, expanded, leaf, row, true);
// editor always selected / focused
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
if (stopCellEditing()) {
fireEditingStopped();
}
}
};
if (editor instanceof JCheckBox) {
((JCheckBox) editor).addItemListener(itemListener);
}
return editor;
}
}
class CheckBoxNode extends DefaultMutableTreeNode{
String text;
boolean selected=false;
public CheckBoxNode(String text) {
this.text = text;
}
public CheckBoxNode(String text, boolean selected) {
this.text = text;
this.selected=selected;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean newValue) {
selected = newValue;
}
public String getText() {
return text;
}
public void setText(String newValue) {
text = newValue;
}
public String toString() {
return "["+ text+"] "+this.getChildCount()+" child(ren)";
}
}
Thanks in advance.