Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes JTree with checkboxes not getting updated question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree with checkboxes not getting updated question" Watch "JTree with checkboxes not getting updated question" New topic
Author

JTree with checkboxes not getting updated question

Dan MacLeod
Ranch Hand

Joined: Mar 19, 2009
Posts: 30
I have a custume renderer that should display a checkbox for my JTree. However, when I select the CheckBox in the JTree it never gets updated (selected). Any ideas? Below is a snippet from the renderer I'm using.

<code>
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {

Component returnValue;
if (leaf) {

String stringValue = tree.convertValueToText(value, selected,
expanded, leaf, row, false);
leafRenderer.setText(stringValue);
leafRenderer.setSelected(false);

leafRenderer.setEnabled(tree.isEnabled());

if (selected) {
leafRenderer.setForeground(selectionForeground);
leafRenderer.setBackground(selectionBackground);
} else {
leafRenderer.setForeground(textForeground);
leafRenderer.setBackground(textBackground);
}

if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
Object userObject = ((DefaultMutableTreeNode) value)
.getUserObject();
if (userObject instanceof CheckBoxNode) {
CheckBoxNode node = (CheckBoxNode) userObject;
leafRenderer.setText(node.getText());
leafRenderer.setSelected(node.isSelected());
}
}
returnValue = leafRenderer;
} else {
returnValue = nonLeafRenderer.getTreeCellRendererComponent(tree,
value, selected, expanded, leaf, row, hasFocus);
}
return returnValue;
}
</code>
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Hi,

This is a bit odd since this is exactly the same renderer as in following link:
http://www.java2s.com/Code/Java/Swing-JFC/CheckBoxNodeTreeSample.htm
and in there, everything works as it should.

I assume, that the bug is not in the renderer ...


Regards,
Rok


SCJP, SCWCD
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Dan MacLeod wrote:I have a custume renderer that should display a checkbox for my JTree. However, when I select the CheckBox in the JTree it never gets updated (selected). Any ideas?


It sounds more like a TreeCellEditor problem, or a TableModel.setValueAt() problem, than a TreeCellRenderer problem.

[edit: I had asked, "Does your setValueAt() handle correctly whatever value the Editor returns when the user toggles the checkbox?" but that would be for JTable, not JTree. Oops.]

bitguru blog
Dan MacLeod
Ranch Hand

Joined: Mar 19, 2009
Posts: 30
For some reason Object userObject = treeNode.getUserObject(); returns a null. In the sample code it returns an object. The only difference is I created my JTree dynamically, the sample code creates a Vector, then creates a JTree passing in the Vector to the JTree constructor.
Dan MacLeod
Ranch Hand

Joined: Mar 19, 2009
Posts: 30
I don't have a "setValueAt()" method, I'm not really sure why I need it. You are correct, I did narrow it down to the TableCellEditor. Here is the editor code that I'm using:

<code>
public 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) {
boolean returnValue = false;
if (event instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent) event;
TreePath path = tree.getPathForLocation(mouseEvent.getX(),
mouseEvent.getY());
if (path != null) {
Object node = path.getLastPathComponent();
if ((node != null) && (node instanceof DefaultMutableTreeNode)) {
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
Object userObject = treeNode.getUserObject();
returnValue = ((treeNode.isLeaf()) && ((userObject instanceof CheckBoxNode) || node instanceof CheckBoxNode));
}
}
}
return returnValue;
}

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;
}
}
</code>
Dan MacLeod
Ranch Hand

Joined: Mar 19, 2009
Posts: 30
I have the issue narrowed down to the getTreeCellEditorComponent() method in the CellEditor.
If I comment out the stopCellEditing() if statement, then the CheckBox will get selected/deselected. This works fine. This caused a
new issue. If I have multiple JCheckBoxes in my JTree, when I select one, the other will become deselected. I'm not sure where
to go from here. Any ideas would be greatly appreciated.

Thanks
Dan


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() {
/**
* Invoked when an item has been selected or deselected by
* the user. The code written for this method performs
* the operations that need to occur when an item
* is selected (or deselected).
*/
public void itemStateChanged(ItemEvent itemEvent) {

//if (stopCellEditing()) {
// fireEditingStopped();
//}
}
};

if (editor instanceof JCheckBox) {
((JCheckBox) editor).addItemListener(itemListener);
}

return editor;
}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Dan, please start using code tags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Dan MacLeod wrote:I don't have a "setValueAt()" method, I'm not really sure why I need it.


Sorry about that. I was thinking JTable, when obviously your question is about JTree.

(With JTable, whatever value is returned by the TableCellEditor is passed to the table's setValueAt() method.)
Dan MacLeod
Ranch Hand

Joined: Mar 19, 2009
Posts: 30
Thanks for the help, I finally got it to work. I had some issues with the CellEditor, but at least I learned something.

thanks again for all the suggestions
Dan
 
 
subject: JTree with checkboxes not getting updated question
 
Similar Threads
Jtree change a node
Radiobuttons in JTree
Checkbox with Jtree
filtering N number of leaf nodes per parent node
disabling the view of files in Jtree