Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to detect if a drop is about to take place on a JTree?

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!

like image 974
sbrattla Avatar asked Nov 13 '22 22:11

sbrattla


1 Answers

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.

like image 122
Howard Avatar answered Feb 15 '23 09:02

Howard