加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – SWT分段表和滚动

发布时间:2020-12-15 02:32:37 所属栏目:Java 来源:网络整理
导读:我正在尝试在表单部分中显示SWT表,但是在列调整大小和滚动时出现问题. 我附上了一些示例代码,可以放在Eclipse插件的ViewPart中(3.7是我正在使用的版本).如果您运行代码,并将列调整到右侧,则表格会获得一个水平滚动条(这就是我想要的). 现在,如果我折叠该部分
我正在尝试在表单部分中显示SWT表,但是在列调整大小和滚动时出现问题.

我附上了一些示例代码,可以放在Eclipse插件的ViewPart中(3.7是我正在使用的版本).如果您运行代码,并将列调整到右侧,则表格会获得一个水平滚动条(这就是我想要的).

现在,如果我折叠该部分(单击“Section Title”左侧的箭头),视图将获得一个水平滚动条(我不想要).再次展开该部分,视图滚动仍然存在,现在该表不再有一个.

我正在寻找一种方法(可能是嵌套复合材料的一些可怕组合)来阻止表格比视图更宽,并且会欣赏任何建议.

public class ExampleView extends ViewPart {
  /** Note: other fields / methods removed to condense snippet */

  private FormToolkit toolkit;
  private ScrolledForm scrolledForm;

  public void createPartControl(Composite parent) {
            parent.setLayout(new FillLayout());

    toolkit = new FormToolkit(parent.getDisplay());
    scrolledForm = toolkit.createScrolledForm(parent);
    scrolledForm.setExpandHorizontal(true);
    scrolledForm.setText("Form Title");

    scrolledForm.getBody().setLayout(new FillLayout());
    // here's the modification for the solution
    scrolledForm.getBody().setLayout(
            new BoundedLayout(new FillLayout(),true));

    final Section section = toolkit.createSection(scrolledForm.getBody(),Section.DESCRIPTION | Section.TITLE_BAR | Section.TWISTIE
                    | Section.EXPANDED);

    section.addExpansionListener(new ExpansionAdapter() {
        public void expansionStateChanged(ExpansionEvent e) {
            scrolledForm.reflow(true);
        }
    });
    section.setText("Section Title");

    final Table table = toolkit.createTable(section,SWT.FULL_SELECTION);
    TableViewer viewer = new TableViewer(table);

    table.setLinesVisible(true);
    table.setHeaderVisible(true);
    table.setItemCount(10);

    TableColumn col = new TableColumn(table,SWT.NONE);
    col.setText("Column A");
    col.setWidth(150);
    col.setResizable(true);
    col.setMoveable(true);

    TableColumn col2 = new TableColumn(table,SWT.NONE);
    col2.setText("Column B");
    col2.setWidth(150);
    col2.setResizable(true);
    col2.setMoveable(true);

    section.setClient(table);
  }
}

解决方法

所以这是我使用自定义布局提出的解决方案.它包装了另一个布局,但是可以将computeSize绑定到父组合的宽度或高度 – 基本上只允许在一个方向上滚动(如果有更简单的方法强制ScrolledForm执行此操作,请告诉我!).

由于computeSize和布局方法受到保护,因此需要应用一些讨厌的反射黑客

import java.lang.reflect.Method;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Layout;

public class BoundedLayout extends Layout {
  protected Layout delegateLayout;

  protected Method computeSizeMethod;
  protected Method layoutMethod;

  protected boolean widthBound;

  public BoundedLayout(Layout delegateLayout,boolean widthBound) {
      setDelegateLayout(delegateLayout);
      this.widthBound = widthBound;
  }

  public Layout getDelegateLayout() {
      return delegateLayout;
  }

  public void setDelegateLayout(Layout delegateLayout) {
      this.delegateLayout = delegateLayout;

      try {
          computeSizeMethod = delegateLayout.getClass().getDeclaredMethod(
                "computeSize",Composite.class,int.class,boolean.class);
          computeSizeMethod.setAccessible(true);

          layoutMethod = delegateLayout.getClass().getDeclaredMethod(
                "layout",boolean.class);
          layoutMethod.setAccessible(true);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
  }

  @Override
  protected Point computeSize(Composite composite,int wHint,int hHint,boolean flushCache) {
    // get comp size to make sure we don't let any children exceed it
    Point compSize = composite.getSize();

    try {
        Point layoutComputedSize = (Point) computeSizeMethod.invoke(
                delegateLayout,composite,wHint,hHint,flushCache);

        if (widthBound) {
            layoutComputedSize.x = Math.min(compSize.x,layoutComputedSize.x);
        } else {
            layoutComputedSize.y = Math.min(compSize.y,layoutComputedSize.y);
        }

        return layoutComputedSize;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
  }

  @Override
  protected void layout(Composite composite,boolean flushCache) {
    try {
        layoutMethod.invoke(delegateLayout,flushCache);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读