2

I have this pretty big project that I'm working on, graphic editor, and I've had some big problems working with swing components. However, now I have a programmers worst nightmare, a bug that is happening only some of the times.

If I try to select the elements in my JTree, whether when adding elements, or when selecting them when they are selected in diagram (JInternalFrame), some of them don't get expanded.

My structure is something like this, I have a workspace, containing projects and projects that contain diagrams. Diagrams hold all the elements I have, let's say circles, rectangles and so on, in folders (if they are circles, they are put in circles folder...).

Another thing to know is that I select my elements via setSelectedPath/Paths method of my JTree.

Some of the things that may help understanding what I do and what I tried:

  • I made sure my nodes know how to get to root. (getTreeModel.getPathToRoot returns good path)
  • I tried adding paths to trees selection model and to tree directly
  • I have set the trees expandsSelectedPaths to true
  • Nodes are selected when I expand my tree manually (they even expand afterwards), until I add new elements of type that caused problems
  • This happens about once when switching through 5 types of elements, and some stranger things happen when I try to add other type of element after I added one that made problems

I hope someone will know what to do, although I think this is very complicated problem. Please ask anything that may help you help me.

Dejan Maksimovic
  • 507
  • 1
  • 5
  • 23
  • are you on a IDE? Try debugger in that and check for the events fired when `JTree` expanded, hope you'll find anything helpful there, if on netbeans, try **Visual Debugger** – Asif Jun 03 '12 at 11:18
  • Ensure all GUI updates are performed on the EDT. If that fails, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jun 03 '12 at 11:24
  • @Asif It fires tree expansion events I catch all expansions using treeExpansionListener, but some of them just aren't presented on GUI. Andrew, I'm afraid that it's too complicated to make a SSCCE, I will do it only if absolutely necessary – Dejan Maksimovic Jun 03 '12 at 11:34

2 Answers2

4

OK, thanks everyone for answering, but I have found a very simple workaround for this problem. The thing was expanding has no effect if the last path component is leaf, for some reason.

What I did was simply making my leaf nodes return false for isLeaf method, and all my problems went away.

Dejan Maksimovic
  • 507
  • 1
  • 5
  • 23
1
  • if addWhatever() to JTree firing the correct TreeModelEvent (fireChildAdded(), firePathChanged(), fireChildrenLoaded(), treeStructureChanged())

  • all changes for GUI would be moved to the BackGround Task(s), please look at SwingWorker or Runnable#Thread (most clear and easiest way), but Runnable#Thread required wrapping all output (Swing methods) to the invokeLater

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • OK, I'm having some trouble understanding, but I guess you want me to check if all these events are called (for the first point), and then to move all changes of tree GUI to `SwingUtilities.invokeLater`? – Dejan Maksimovic Jun 03 '12 at 12:15
  • if you add a new path from JTree view, then by default all changes are in TreeModel too, but in the case that you loading JTree structure from Xml, File, Database, then all updates to the TreeModel should be wrapper into invokeLater – mKorbel Jun 03 '12 at 12:36
  • Yes, `JTree` is particularly good at exposing EDT problems; this [question](http://stackoverflow.com/q/7787998/230513) cites ways to detect such violations. See also [Initial Threads](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Jun 03 '12 at 13:44
  • I have tried some of the things that you guys suggested: My tree is not having problems with adding elements, but with expanding. So I believe its enough to check if it's sending valid events for expanding, and it is, and they are all happening on EDT. However, once it tries to expand, and it doesn't expand, it won't try again. That's all I found out using fireTreeExpanded method. I tried CheckThreadViolationRepaintManager, and it just throws a lot of exceptions when loading main window, and it doesn't complain later. – Dejan Maksimovic Jun 03 '12 at 14:41
  • no idea whats happend, post an SSCCE demostrated your issue, otherwise everything are shots to the dark – mKorbel Jun 03 '12 at 17:22