<Swing Dev> [10][JDK-8187936] Automatically selecting a new JTree node in a model listener can cause unusual behavior.

Krishna Addepalli krishna.addepalli at oracle.com
Wed Nov 8 14:51:59 UTC 2017

Hi Sergey,

Could you review this and let me know your feedback?





From: Krishna Addepalli 
Sent: Friday, October 27, 2017 4:43 PM
To: swing-dev at openjdk.java.net
Subject: [10][JDK-8187936] Automatically selecting a new JTree node in a model listener can cause unusual behavior.


Hi All,

Please review the help text that is updated for this bug:


Bug: JDK-8187936: https://bugs.openjdk.java.net/browse/JDK-8187936


Webrev: http://cr.openjdk.java.net/~kaddepalli/8187936/webrev00/



As the bug title mentions, this is an unrecommended way of using the model listeners. The code posted in the bug tries to update the JTree path in the model listener callback, and since the JTree is yet to change itself to the underlying model, it results in weird UI behavior.

Attached code in the bug is corrected and re-uploaded. 


This typically happens since listeners are called in a particular order (either last to first or first to last in the order of registration), and if the model listener tries to change the GUI before the GUI has had a chance to react itself to the changes in the underlying model. For example, highlighting a selection path for a node added in the JTree, in the TreeModelListener callback could lead to an extra node being added or existing nodes not being shown, since JTree would not have yet updated its state based on the model changes.


In such cases it is recommended to wrap the callback function contents into a lambda, and invoke it through "SwingUtilities.invokeLater". This ensures that all the UI elements have had a chance to react to the model changes, and any UI actions will be guaranteed to operate on the updated widgets.


Similar update has been done in package-info.java for Swing, so that it acts as a reference.




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/swing-dev/attachments/20171108/91bf21b5/attachment.html>

More information about the swing-dev mailing list