JFormDesigner / FlatLaf

FlatLaf - Swing Look and Feel (with Darcula/IntelliJ themes support)
https://www.formdev.com/flatlaf/
Apache License 2.0
3.31k stars 266 forks source link

JTree#getPathForLocation() doesn't work properly if 'Tree.wideSelection' is enabled #373

Open tristanzhou opened 3 years ago

tristanzhou commented 3 years ago

Hi,

The mouse location of the MouseEvent was converted to a new X coordinate if the 'Tree.wideSelection' is enabled in FlatTreeUI. But we may still pass the original location of MouseEvent in other mouse listeners to JTree#getPathForLocation() for getting a TreePath. It only works if you click on the text area of the tree node, but not on the highlighted blank area. Any idea of this?

Thanks. Tristan

DevCharly commented 3 years ago

An alternative would be JTree.getClosestPathForLocation(), which works fine with wide selection.

The downside of this method is that if clicking on the free area below the tree (e.g. if all nodes collapsed), then this method returns the last shown node (instead of null). To avoid this, you could get the bounds of the found node and check whether mouse Y coordinate is within the node bounds.

Anyway, I've made a change to FlatLaf in commit 268fe15004f59e19680f9ad7a0a46d52eda368a4 that "fixes" JTree.getPathForLocation() for wide selection.

Because I'm not sure what consequence this has for existing applications, I've disabled this by default. Have to test some larger apps that use FlatLaf before enabling this by default... Use UIManager.put( "FlatLaf.experimental.tree.widePathForLocation", true ); to enable it in your app.