有这样一个需求,左右各一个表格,要求拖动其中任意一个表格中的滚动条,另外一个都随之滚动,看起来就像是在一个表格中。

本文详细介绍了如何在两个TableViewer中实现滚动条联动,确保左边表与右边表的选择一致,包括设置滚动条同步、选择高亮效果及在Windows系统上的选择背景绘制。同时,实现了左右两边垂直滚动条的同步滚动,保证了用户体验的一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
  1. /** *//**  
  2.        * 设置左边(右边)表格的滚动条根据右边(左边)滚动条滚动而滚动  
  3.        */  
  4.       // Make selection the same in both tables   
  5.       tParameterAlarm1.addListener(SWT.Selection, new Listener() ...{   
  6.           public void handleEvent(Event event) ...{   
  7.               tParameterAlarm2.setSelection(tParameterAlarm1.getSelectionIndices());   
  8.           }   
  9.       });   
  10.       // On Windows, the selection is gray if the table does not have focus.   
  11.       // To make both tables appear in focus, draw teh selection background   
  12.       // here.   
  13.       // This part only works on version 3.2 or later.   
  14.       Listener eraseListener = new Listener() ...{   
  15.           public void handleEvent(Event event) ...{   
  16.               if ((event.detail & SWT.SELECTED) != 0) ...{   
  17.                   GC gc = event.gc;   
  18.                   Rectangle rect = event.getBounds();   
  19.                   gc.setForeground(container.getDisplay().getSystemColor(   
  20.                                                         SWT.COLOR_LIST_SELECTION_TEXT));   
  21.                   gc.setBackground(container.getDisplay().getSystemColor(   
  22.                                                         SWT.COLOR_LIST_SELECTION));   
  23.                   gc.fillRectangle(rect);   
  24.                   event.detail &= ~SWT.SELECTED;   
  25.               }   
  26.           }   
  27.       };   
  28.   
  29.       tParameterAlarm1.addListener(SWT.EraseItem, eraseListener);   
  30.       // Make vertical scrollbars scroll together   
  31.       ScrollBar vBarLeft = tParameterAlarm1.getVerticalBar();   
  32.       vBarLeft.addListener(SWT.Selection, new Listener() ...{   
  33.           public void handleEvent(Event event) ...{   
  34.               tParameterAlarm2.setTopIndex(tParameterAlarm1.getTopIndex());   
  35.           }   
  36.       });   
  37.       tParameterAlarm2.addListener(SWT.Selection, new Listener() ...{   
  38.           public void handleEvent(Event event) ...{   
  39.               tParameterAlarm1.setSelection(tParameterAlarm2.getSelectionIndices());   
  40.           }   
  41.       });   
  42.       tParameterAlarm2.addListener(SWT.EraseItem, eraseListener);   
  43.       ScrollBar vBarRight = tParameterAlarm2.getVerticalBar();   
  44.       vBarRight.addListener(SWT.Selection, new Listener() ...{   
  45.           public void handleEvent(Event event) ...{   
  46.               tParameterAlarm1.setTopIndex(tParameterAlarm2.getTopIndex());   
  47.           }   
  48.       });  

其中tParameterAlarm1为左边表格的TableViewer,tParameterAlarm2为右边表格的TableViewer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值