JList中,当鼠标移动到某一项上时就高亮显示,该怎么做这个事件?

解决方案 »

  1.   

    Point lastPoint;
    jlist.addMouseListener(new MouseAdapter(){
        @Override public void mouseEntered(MouseEvent e){
            lastPoint = e.getPoint();
        }
    };
    jlist.addMouseMotionListener(new MouseMotionAdapter(){
        @Override public void mouseMoved(MouseEvent e){
            int lastIndex = jlist.locationToIndex(lastPoint);
            Point current = e.getPoint();
            int currentIndex = jlist.locationToIndex(current);
            if (currentIndex == lastIndex) return;        ... // highlight
            lastPoint = current;
        }
    };
    就是这个想法。
      

  2.   


    hisList.addMouseMotionListener(new MouseMotionAdapter(){
        @Override public void mouseMoved(MouseEvent e){
            Point current = e.getPoint();
            int currentIndex = hisList.locationToIndex(current);
            hisList.setSelectedIndex(currentIndex);
        }
    });
      

  3.   

    jList.setSelectionBackground(Color.**);
    jList.setSelectionForeground(Color.**);
    试试吧,就知道这些