How do I display an edited JTree node in bold face?

Sandip Chitale

You will need a combination of TreeCellEditor and TreeCellRenderer implementations.

When the user clicks on a TreeNode (or performs any other gesture that has been configured to start editing) the TreeCellEditor kicks in. The user edits the value. When the user commits the value the TreeCellEditor's -

public boolean stopCellEditing()
    Tell the editor to stop editing and
    accept any partially edited value as the
    value of the editor. The editor returns
    false if editing was not stopped, useful for
    editors which validates and can not accept
    invalid entries.

is called. You could note down the TreeNode that was modified as part of the TreeNode implementation state or in the userObject inside the TreeNode.

You could subclass DefaultMutableTreeNode or implement TreeNode, have a property "modified".

If the user does not commit the changes then the modified flag should not be set.

For showing the bold font you could do the following -

Install a subclass of DefaultTreeCellRenderer. In the overridden method getTreeCellRendererComponent(..) do -

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

  JLabel label = super.getTreeCellRenderer(...);
  if (((Cast here) value).isModified()) {
    label.setFont(/* bold font */);
  return label;

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact