此处用的是CheckboxTreeViewer  和treeview大同小异

 

首先获得Point

tree.addMouseListener(new MouseListener() {
   
   @Override
   public void mouseUp(MouseEvent e) {
    
   }
   
   @Override
   public void mouseDown(MouseEvent e) {
    point = new Point(e.x, e.y);
   }
   
   @Override
   public void mouseDoubleClick(MouseEvent e) {
    
   }
  });

 

其次根据point获得TreeItem  再变色

tv.addCheckStateListener(new ICheckStateListener() {
   public void checkStateChanged(CheckStateChangedEvent event) {
    // If the item is checked . . .
    TreeItem ti = tree.getItem(point);

    // 也可以不用point

    // TreeItem ti = ((TreeItem) tv.testFindItem(event.getElement()));
    if (event.getChecked()) {
     tv.setSubtreeChecked(event.getElement(), true);
     ti.setBackground(0, Display.getCurrent().getSystemColor(SWT.COLOR_RED)); //获得当前行的第一列 并设置RED色
    } else {
     tv.setSubtreeChecked(event.getElement(), false);
     ti.setBackground(0, Display.getCurrent().getSystemColor(SWT.COLOR_CYAN)); //获得当前行的第一列 并设置CYAN色

    }
   }
  });