想要在JTree列出的元素上更改鼠标移动上的光标

时间:2013-01-25 12:53:20

标签: java swing cursor jlist jtree

当光标仅在列出的元素上时,我希望将光标更改为鼠标移动到JTree组件上的手形光标,而不是整个组件。

以下代码适用于Jlist组件。我希望JTree使用相同内容,但JTree 没有 getCellBound()。还有其他办法吗?

final JList list = new JList(new String[] {"a","b","c"});
list.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
    final int x = e.getX();
    final int y = e.getY();
    // only display a hand if the cursor is over the items
    final Rectangle cellBounds = list.getCellBounds(0, list.getModel().getSize() - 1);
    if (cellBounds != null && cellBounds.contains(x, y)) {
        list.setCursor(new Cursor(Cursor.HAND_CURSOR));
    } else {
        list.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }
}

@Override
public void mouseDragged(MouseEvent e) {
}
});

2 个答案:

答案 0 :(得分:4)

你正在寻找这样的东西,我想:

import java.awt.Cursor;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class TestTreeSelection {

    protected void initUI() {
        final DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
        fillTree(root, 5, "Some tree label");
        final DefaultTreeModel model = new DefaultTreeModel(root);
        final JTree tree = new JTree(model);
        tree.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                boolean inside = false;
                TreePath path = tree.getPathForLocation(e.getX(), e.getY());
                if (path != null) {
                    Rectangle pathBounds = tree.getPathBounds(path);
                    inside = pathBounds.contains(e.getPoint());
                }
                if (inside) {
                    tree.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                } else {
                    tree.setCursor(Cursor.getDefaultCursor());
                }
            }
        });
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JScrollPane(tree));
        f.setSize(400, 600);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private void fillTree(DefaultMutableTreeNode parent, int level, String label) {
        for (int i = 0; i < 5; i++) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(label + " " + i);
            parent.add(node);
            if (level > 0) {
                fillTree(node, level - 1, label);
            }
        }
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTreeSelection().initUI();
            }
        });
    }

}

答案 1 :(得分:0)

此代码有效:

        tree_object.addMouseMotionListener(new MouseMotionListener() {

                @Override
                public void mouseDragged(MouseEvent e) {

                }

                @Override
                public void mouseMoved(MouseEvent e) {
                    TreePath tp = ((JTree)e.getSource()).getPathForLocation(e.getX(), e.getY());

                    if(tp != null)
                    {
                        ((JTree)e.getSource()).setCursor(new Cursor(Cursor.HAND_CURSOR));
                    }
                    else
                    {
                        ((JTree)e.getSource()).setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    }
                }
         });