Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2006

Tip: Looking for answers? Try searching our database.

JTree display update issue with new nodes

Thread view: 
qu0ll - 27 Sep 2006 13:17 GMT
I have a problem with the following code.  The code is supposed to create a
new node and add it as a child to another node in a JTree.  This is indeed
happening but the tree is only updating the display and selecting the new
node the first time a node is added.  For each subsequent child node, it
appears as though the child node is added successfully but it is just not
appearing in the tree.  If I check getChildCount() on the parent node after
each child is added it reveals the correct number so it seems to be a
display issue.  Could someone please point out the mistake in the following
code?

This code exists in a control class (not in the JTree itself).  The variable
parentNode is a DefaultMutableTreeNode passed in as a parameter:

DefaultMutableTreeNode childNode = new DefaultMutableTreeNode();
parentNode.add(childNode);
tree.setSelectionPath(new TreePath(childNode.getPath()));
MyTreeModel model = (MyTreeModel)tree.getModel();
model.nodesWereInserted(parentNode, new int[] {parentNode.getChildCount() -
1});
tree.repaint();

This is the tree model:

public class MyTreeModel extends DefaultTreeModel
{
  private DefaultMutableTreeNode root;
  private ArrayList<TreeModelListener> listeners = new
ArrayList<TreeModelListener>();

  public MyTreeModel(DefaultMutableTreeNode root)
  {
     super(root);
     this.root = root;
  }

  public void addTreeModelListener(TreeModelListener l)
  {
     listeners.add(l);
  }

  public Object getChild(Object parent, int index)
  {
     return ((DefaultMutableTreeNode)parent).getChildAt(index);
  }

  public int getChildCount(Object parent)
  {
     return ((DefaultMutableTreeNode)parent).getChildCount();
  }

  public int getIndexOfChild(Object parent, Object child)
  {
     return
((DefaultMutableTreeNode)parent).getIndex((DefaultMutableTreeNode)child);
  }

  public Object getRoot()
  {
     return root;
  }

  public boolean isLeaf(Object node)
  {
     return ((DefaultMutableTreeNode)node).isLeaf();
  }

  public void removeTreeModelListener(TreeModelListener l)
  {
     listeners.remove(l);
  }

  public void valueForPathChanged(TreePath path, Object newValue)
  {
  }
}

Signature

And loving it,

qu0ll
______________________________________________
qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email)

Bart Cremers - 27 Sep 2006 14:20 GMT
> I have a problem with the following code.  The code is supposed to create a
> new node and add it as a child to another node in a JTree.  This is indeed
[quoted text clipped - 13 lines]
>
> qu0ll

The problem lies in the fact that you're not correctly implemting
TreeModel functionality (listener notification to be specific in this
case). You're several methods of the DefaultTreeModel in your subclass.
Simply don't override these methods and let the DefaultTreeModel handle
this stuff (nodesWereInserted) and it will work.

You even don't need to use a custom tree model for the functionality
you're using right now. Simply replace it with a DefaultTreeModel and
it'll work.

Regards,

Bart
qu0ll - 27 Sep 2006 15:51 GMT
>> I have a problem with the following code.  The code is supposed to create
>> a
[quoted text clipped - 28 lines]
> you're using right now. Simply replace it with a DefaultTreeModel and
> it'll work.

OK, thanks for that.  I replaced my custom tree model with the default and
it works - mostly!  Now the new nodes show up but for some reason there is a
blank "line" displayed in the tree like this:

Root

* Node
|  |
|  * Node
|     |
|     * Node
| <blank space>
* Node
  |
  * Node

Why the blank space?  How do I get rid of it?

Signature

And loving it,

qu0ll
______________________________________________
qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email)

qu0ll - 27 Sep 2006 16:12 GMT
<snip>

> OK, thanks for that.  I replaced my custom tree model with the default and
> it works - mostly!  Now the new nodes show up but for some reason there is
[quoted text clipped - 13 lines]
>
> Why the blank space?  How do I get rid of it?

Further to this, I have found that if I remove the line of code to select
the new node, it works fine but I have to expand the parent node to see the
new node.  Why would selecting the new node cause a blank line to appear?  I
need to have the new node automatically selected.

Signature

And loving it,

qu0ll
______________________________________________
qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email)

qu0ll - 27 Sep 2006 16:34 GMT
> <snip>
>
[quoted text clipped - 20 lines]
> the new node.  Why would selecting the new node cause a blank line to
> appear?  I need to have the new node automatically selected.

Solved it! It seems that trying to select the new node before calling
nodesWereInserted() is a problem.  So I moved the selection to after the
call and it works perfectly.  Thanks for your help.

Signature

And loving it,

qu0ll
______________________________________________
qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email)

Dan Andrews - 27 Sep 2006 14:25 GMT
> I have a problem with the following code.  The code is supposed to create a
> new node and add it as a child to another node in a JTree.  This is indeed
[quoted text clipped - 20 lines]
>
> public class MyTreeModel extends DefaultTreeModel

Hi qu0ll,

Looks like you might have originally started implementing TreeModel
from scratch and then decided to use the already build functionality by
extending DefaultTreeModel. For now just extend the DefaultTreeModel
with no implementation (e.g. public class MyTreeModel extends
DefaultTreeModel{/*empty*/}) and see what the basic behaviour is before
overriding or providing new functionality.

Cheers,

Dan Andrews
- - - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited http://www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - - -
qu0ll - 27 Sep 2006 15:53 GMT
>> I have a problem with the following code.  The code is supposed to create
>> a
[quoted text clipped - 35 lines]
> DefaultTreeModel{/*empty*/}) and see what the basic behaviour is before
> overriding or providing new functionality.

Thanks Dan.  Please see my recent reply to Bart about this.  You are
absolutely spot on.

Signature

And loving it,

qu0ll
______________________________________________
qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email)

Dan Andrews - 27 Sep 2006 16:39 GMT
> Thanks Dan.  Please see my recent reply to Bart about this.  You are
> absolutely spot on.

Hi qu0ll,

(1) Your code does this and a blank node is displayed (no userObject is
specified):
  DefaultMutableTreeNode childNode = new DefaultMutableTreeNode();

(2) The DefaultTreeCellRenderer does this:
  String stringValue = tree.convertValueToText(value, sel,
    expanded, leaf, row, hasFocus);
  setText(stringValue);

(3) And the JTree's convertValueToText does this:
  if(value != null) {
    String sValue = value.toString();
    if (sValue != null) {
      return sValue;
    }
  }
  return "";

(4) And the DefaultMutableTreeNode is the value and has no userObject so
null is returned in it's toString like this:
  if (userObject == null) {
    return null;
  } else {
    return userObject.toString();
  }

Cheers,

Dan Andrews
- - - - - - - - - - - - - - - - - - - - - - - - -
Ansir Development Limited http://www.ansir.ca
- - - - - - - - - - - - - - - - - - - - - - - - -


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.