开发者

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜