知识库 : FlatScrollBar滚动条控件的使用

Edit Document

 

 

 

 

 

 

 

 

 

FlatScrollBar滚动条控件的使用

 

 

 

作者 :张青松


目录

1.               FlatScrollBar 滚动条

1.1.               添加滚动条

1.1.1.               普通方法添加滚动条

1.1.2.               Table 与 Tree 添加滚动条

1.2.               Codeffine.jar 包中其他有用的类

 

1.     FlatScrollBar 滚动条

FlatScrollBar 是一个第三方滚动条控件,在 org.knime.workbench.core 项目中引入的 codeaffine.jar 包中。如图:

 

图 1  codeaffine.jar 包

 

使用 FlatScrollBar 可以在在任意位置添加滚动条,如图

 

     图 2 下方和右边 FlatScrollBar 滚动条

图 3 下方的 FlatScrollBar 滚动条

1.1.     添加滚动条

FlatScrollBar 滚动条提供多种方式添加,可以如 SWT 的普通组件一样添加,针对 Table 和 Tree 控件, FlatScrollBar 提供了现有的包装类 com.codeaffine.eclipse.swt.widget.scrollable FlatScrollBarTable 和 com.codeaffine.eclipse.swt.widget.scrollable . FlatScrollBarTree 类方便的添加滚动条。

1.1.1. 普通方法添加滚动条

针对下图中的 Main 的类型选择滚动条的添加方式,对于简单的 Composite 等控件,选择这种方式,具体参考 ModelView 中滚动条的添加。

添加滚动条如图:

 

FlatScrollBar 需要设置当前 Main 与 Parent 的 Height 和 Weight 的比例计算是否需要显示滚动条,以及滚动条的长度。

和其他 SWT 组件一样的方法初始化 FlatScrollBar 对象,并且将滚动条通过布局放到的 Parent 容器的对应位置。

 

FlatScrollBar v Scroll = new FlatScrollBar(parent, SWT. V_SCROLL );

FormData fd = new FormData();

fd. width = 16;

fd. height = parent.getSize(). y ;

fd. top = new FormAttachment(0, 0);

fd. right = new FormAttachment(100, 0);

fd. bottom = new FormAttachment(100, 0);

v Scroll .setLayoutData(fd);

// 当滚动条位置变化时, Main 滚动到相应位置

v Scroll .addSelectionListener( new SelectionAdapter() {

                            @Override

                            public void widgetSelected(SelectionEvent event) {

                                          Control c = Mian ;

                                          Point location = c.getLocation();

                                          c.setLocation(location. x , - v Scroll .getSelection());

                            }

              });

ControlListener listener = new FocusControlListener( Mian , v Scroll );

// 对 Main 控件添加监听,当 Main 的 Size 发生变化时,重新计算滚动条的长度

Mian .addControlListener(listener);

其中的监听 Listener 类如下:

private class FocusControlListener implements ControlListener {

                            private Control control ;

                            // 解决滚动条对象不一致问题

                            private FlatScrollBar bar ;

                            public FocusControlListener (Control control, FlatScrollBar bar) {

                                          this . control = control;

                                          this . bar = bar;

                            }

 

                            @Override

                            public void controlMoved(ControlEvent arg0) {

                            }

 

                            /**

                            * 设置滚动条

                            */

                            @Override

                            public void controlResized(ControlEvent arg0) {

                                          if ( bar != null && control != null ) {

                                                        bar .setMaximum( control .getSize(). y ); // 设置需要滚动的控件总长度                                                         bar .setThumb( control .getParent().getClientArea(). height ); // 设置能显示的控件宽度                                                         bar .setPageIncrement( control .getParent().getClientArea(). height ); // 同上

                                                        control .setLocation(0, control .getLocation(). y ); // 显示 pageContentComposite 的最前端

                                                        bar .setSelection(0); // 使滚动条回到最前端

                                          }

                            }

              }

 

在 Listener 中通过滚动条的 setMaximum () 、 setPageIncrement () 和 setPageIncrement () 方法设置滚动条。其中 setMaximum () 传入 Main 的 height ,而 setPageIncrement () 与 setPageIncrement () 都传入 Parent 的 height 。

1.1.2. Table 与 Tree 添加滚动条

对于 Main 是 Tree 或者 Table 这样较为复杂的情况 ,提供了另一种封装好的类,位于 com.codeaffine.eclipse.swt.widget.scrollable 包中的 FlatScrollBarTable 和 FlatScrollBarTree 。

以 FlatScrollBarTree 举例说明使用方法 。 具体使用实例参见 ExplorerView 中滚动条的添加方式 。

 

  1. 继承 ScrollableFactory 重新 create 方法得到 Tree 对象

public class TreeScroFactory implements ScrollableFactory<Tree>{

 

              @Override

              public Tree create(Composite parent) {

// 同上 生成 Tree 对象

                                          TreeViewer m_viewer = createTreeView(parent);

                                          Tree tree = m_viewer .getTree();

                                          return tree;

              }

}

  1. 用 FlatScrollBarTree 对象关联 Tree 与 Parent

ScrollableFactory<Tree> factory = new TreeScroFactory();

new FlatScrollBarTree( parent , factory);

 

针对其他 Main 组件是较为复杂的类型是,可以仿照 FlatScrollBarTree 自己的类 。

1.2.     C odeffine.jar 包中其他有用的类

在 codeffine . Jar 包中还有一个封装了 FormLayout 布局的工具类

通过 FormDatas 工具类可以方便的,以简短的代码完成 FormLayout 布局。使用举例:

FormDatas. attach ( control_A ).atLeftTo( control_B , 10, SWT. TOP )

.atTopTo( control_C ,12);

Attachments:

知识库文档模板.docx (application/vnd.openxmlformats-officedocument.wordprocessingml.document)