493: Undecipherable

-Blog-

-Projects-

-About me-

-RSS-

JTree on an JScrollPane and resizing

Dennis Guse

Early on the morning I read my early morning bug reports ;). It was about an JTree on a JScrollPane. The model of the will be modified during runtime and fires events if something is changes. So far everything looks pretty straight forward and it works. The problem occured if the tree gots larger than the parent scrollpane, so I believed the scrollpane starts to activate the scrollbars and everything stays cool… But the tree never changed it’s size. It simple stayed in the old size and the scrollpane didn’t start scrolling. And so some items of the tree were invisible.

1
2
3
4
TreeModel model = new DefaultTreeModel();
JTree tree = new JTree(model);
tree.setPreferredSize(new Dimension(100, 100));
JScrollPane pane = new JScrollPane(tree);

The model fires an TreeModelEvent (DefaultTreeModel.nodeStructureChanged) and the tree get’s an JTree.treeDidChange() so it can recompute it’s size. This doesn’t work out. It was necessary to set the preferred size of the tree to null before excecuting treeDidChange… Why? I honestly don’t know…