2013-01-25 45 views
1

我想要更改光標鼠標移動到JTree組件上的手形光標僅當列出的元素,而不是整個組件。以下代碼適用於Jlist組件。我想同樣爲JTree,但JTree沒有getCellBound()。有沒有其他方法?想要改變鼠標移動JTree列出的元素

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) { 
} 
}); 
+0

爲了更好地幫助越早,張貼[SSCCE(HTTP:// sscce.org/)。 –

+0

我不明白問題所在。它似乎爲我工作。而你的標題說「JTree」,但你的代碼說「JList」:它是哪一個? –

+0

@Guillaume Polet:忘記代碼,只需要當光標在顯示的元素上移動而不是當光標在JTree組件的任何部分上時,只需要Jtree組件上的手形光標,而默認情況下它會在JTree –

回答

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(); 
      } 
     }); 
    } 

} 
+0

thanx很多......它的作品 –

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)); 
        } 
       } 
     }); 
相關問題