• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTree with checkboxes not getting updated question

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.]
 
Dan MacLeod
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan, please start using code tags.
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic