I have a JTree where users can drop elements from other components. When the users hovers over nodes in the tree (during "drop mode") the most near lying node is highlighted. This is achieved in the implementation of TransferHandler.
@Override
public boolean canImport(TransferSupport support) {
//Highlight the most near lying node in the tree as the user drags the
//mouse over nodes in the tree.
support.setShowDropLocation(true);
Each time a new node is selected (also during "drop mode"), this will kick of a TreeSelectionEvent. This in turn will invoke a listener i have created which will query a database for details related to that node.
Now, I am looking for a way to somehow filter out events that is generated from node selections during "drop mode". It is an attempt to limit database calls. Does anyone have any ideas about how I can achieve this?
All input will be highly appreciated!
There is a very indirect method to detect this case. You may register a PropertyChangeListener
on the property "dropLocation"
with the tree component. This will be called whenever the drop location changes and thus you can set a field dropOn
there which you then can read in the TreeSelectionListener
.
tree.addPropertyChangeListener("dropLocation", new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent pce) {
dropOn = pce.getNewValue() != null;
}
});
tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent tse) {
System.out.println(tse + " dropOn=" + dropOn);
}
});
Note that this does fire a wrong false
value for the first time it enters the tree, but all subsequent events then show dropOn = true
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With