EclipseRCP开发之如何让左右两边两个表格协同滚动

出自:http://blog.csdn.net/kevin99zhang/archive/2007/10/25/1843463.aspx
  有这样一个需求,左右各一个表格,要求拖动其中任意一个表格中的滚动条,另外一个都随之滚动,看起来就像是在一个表格中。
具体实现如下:
  /** *//**
         * 设置左边(右边)表格的滚动条根据右边(左边)滚动条滚动而滚动
         */
        // Make selection the same in both tables
        tParameterAlarm1.addListener(SWT.Selection, new Listener() ...{
            public void handleEvent(Event event) ...{
                tParameterAlarm2.setSelection(tParameterAlarm1.getSelectionIndices());
            }
        });
        // On Windows, the selection is gray if the table does not have focus.
        // To make both tables appear in focus, draw teh selection background
        // here.
        // This part only works on version 3.2 or later.
        Listener eraseListener = new Listener() ...{
            public void handleEvent(Event event) ...{
                if ((event.detail & SWT.SELECTED) != 0) ...{
                    GC gc = event.gc;
                    Rectangle rect = event.getBounds();
                    gc.setForeground(container.getDisplay().getSystemColor(
                                                          SWT.COLOR_LIST_SELECTION_TEXT));
                    gc.setBackground(container.getDisplay().getSystemColor(
                                                          SWT.COLOR_LIST_SELECTION));
                    gc.fillRectangle(rect);
                    event.detail &= ~SWT.SELECTED;
                }
            }
        };

        tParameterAlarm1.addListener(SWT.EraseItem, eraseListener);
        // Make vertical scrollbars scroll together
        ScrollBar vBarLeft = tParameterAlarm1.getVerticalBar();
        vBarLeft.addListener(SWT.Selection, new Listener() ...{
            public void handleEvent(Event event) ...{
                tParameterAlarm2.setTopIndex(tParameterAlarm1.getTopIndex());
            }
        });
        tParameterAlarm2.addListener(SWT.Selection, new Listener() ...{
            public void handleEvent(Event event) ...{
                tParameterAlarm1.setSelection(tParameterAlarm2.getSelectionIndices());
            }
        });
        tParameterAlarm2.addListener(SWT.EraseItem, eraseListener);
        ScrollBar vBarRight = tParameterAlarm2.getVerticalBar();
        vBarRight.addListener(SWT.Selection, new Listener() ...{
            public void handleEvent(Event event) ...{
                tParameterAlarm1.setTopIndex(tParameterAlarm2.getTopIndex());
            }
        });

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

你可能感兴趣的:(.net,windows,Blog)