is Mouse on JTree Root Item Position - Java Swing

Java examples for Swing:JTree

Description

is Mouse on JTree Root Item Position

Demo Code


//package com.java2s;
import java.awt.event.MouseEvent;

import javax.swing.JTree;

import javax.swing.tree.TreePath;

public class Main {
    /**/*from  ww w .ja  v  a  2 s.  c om*/
     * Liefert, ob der Mauszeiger ?ber dem Wurzelitem steht.
     *
     * @param  evt  Mausereigenis
     * @return true, falls der Zeiger ?ber dem Wurzelitem steht
     */
    public static boolean isRootItemPosition(MouseEvent evt) {
        if (evt == null) {
            throw new NullPointerException("evt == null");
        }
        if (evt.getSource() instanceof JTree) {
            JTree tree = (JTree) evt.getSource();
            TreePath mousePath = tree.getPathForLocation(evt.getX(),
                    evt.getY());
            if (mousePath != null) {
                Object root = tree.getModel().getRoot();
                Object mouseItem = mousePath.getLastPathComponent();
                if ((root != null) && (mouseItem != null)) {
                    return root.equals(mouseItem);
                }
            }
        }
        return false;
    }
}

Related Tutorials