Is there a way to determine if a crash will occur on JTree?

I have a JTree where users can drop items from other components. When users hang over nodes in the tree (during the "drop mode"), the one closest to the node is highlighted. This is achieved by implementing 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 delete the TreeSelectionEvent. This, in turn, will call the listener I created, which will query the database for details related to this node.

Now I'm looking for a way to somehow filter out the events that are generated from the node during the "drop mode". This is an attempt to restrict database calls. Anyone have any ideas on how I can achieve this?

All input will be appreciated!

+5
source share
1 answer

There is a very indirect method to detect this case. You can register PropertyChangeListenerin a property "dropLocation"using the tree component. This will be called whenever the drag location changes, and so you can set a field dropOnthat you can then read into 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);
    }
});

Please note that this is triggered if the value is incorrect falsewhen entering the tree, but all subsequent events show dropOn = true.

+1
source

All Articles