【问题标题】:Prevent SWT ScrolledComposite from eating part of it's children防止 SWT ScrolledComposite 吃掉它的部分孩子
【发布时间】:2010-09-07 07:12:15
【问题描述】:

我做错了什么?

这是我的代码的摘录:

public void createPartControl(Composite parent) {
  parent.setLayout(new FillLayout());
  ScrolledComposite scrollBox = new ScrolledComposite(parent, SWT.V_SCROLL);
  scrollBox.setExpandHorizontal(true);
  mParent = new Composite(scrollBox, SWT.NONE);
  scrollBox.setContent(mParent);
  FormLayout layout = new FormLayout();
  mParent.setLayout(layout);
  // Adds a bunch of controls here
  mParent.layout();
  mParent.setSize(mParent.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
}

...但它会剪辑最后一个按钮:

bigbrother82:那没用。

SCdF:我试过你的建议,现在滚动条不见了。我需要在这方面做更多的工作。

【问题讨论】:

  • 嘿杰里米,我现在才注意到我的建议没有奏效。你有什么运气吗?
  • (顺便说一句,如果您对我的回答发表评论,那么它将显示在我的回复中,我不会错过)

标签: java eclipse swt rcp


【解决方案1】:

这是使用ScrolledComposite 时的常见障碍。当它变得太小以至于必须显示滚动条时,客户端控件必须水平收缩以为滚动条腾出空间。这具有使一些标签换行的副作用,这会使以下控件向下移动,从而增加了内容组合所需的最小高度。

您需要监听内容组合 (mParent) 上的宽度变化,在给定新内容宽度的情况下再次计算最小高度,然后在具有新高度的滚动组合上调用 setMinHeight()

public void createPartControl(Composite parent) {
  parent.setLayout(new FillLayout());
  ScrolledComposite scrollBox = new ScrolledComposite(parent, SWT.V_SCROLL);
  scrollBox.setExpandHorizontal(true);
  scrollBox.setExpandVertical(true);

  // Using 0 here ensures the horizontal scroll bar will never appear.  If
  // you want the horizontal bar to appear at some threshold (say 100
  // pixels) then send that value instead.
  scrollBox.setMinWidth(0);

  mParent = new Composite(scrollBox, SWT.NONE);

  FormLayout layout = new FormLayout();
  mParent.setLayout(layout);

  // Adds a bunch of controls here

  mParent.addListener(SWT.Resize, new Listener() {
    int width = -1;
    public void handleEvent(Event e) {
      int newWidth = mParent.getSize().x;
      if (newWidth != width) {
        scrollBox.setMinHeight(mParent.computeSize(newWidth, SWT.DEFAULT).y);
        width = newWidth;
      }
    }
  }

  // Wait until here to set content pane.  This way the resize listener will
  // fire when the scrolled composite first resizes mParent, which in turn
  // computes the minimum height and calls setMinHeight()
  scrollBox.setContent(mParent);
}

在监听大小变化时,请注意我们会忽略宽度保持不变的任何调整大小事件。这是因为内容高度的变化不会影响内容的最小高度,只要宽度相同。

【讨论】:

    【解决方案2】:

    如果我没记错的话,你需要换

    mParent.layout();
    

    mParent.setSize(mParent.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
    

    这样你就有了:

    public void createPartControl(Composite parent) {
      parent.setLayout(new FillLayout());
      ScrolledComposite scrollBox = new ScrolledComposite(parent, SWT.V_SCROLL);
      scrollBox.setExpandHorizontal(true);
      mParent = new Composite(scrollBox, SWT.NONE);
      scrollBox.setContent(mParent);
      FormLayout layout = new FormLayout();
      mParent.setLayout(layout);
      // Adds a bunch of controls here
      mParent.setSize(mParent.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
      mParent.layout();
    }
    

    【讨论】:

      【解决方案3】:

      布局后不需要重新计算scrollBox的大小吗?

      【讨论】:

        【解决方案4】:

        布局完成后,尝试在 ScrolledComposite 上设置 .setMinWidth 和 .setMinHeight,并将其传递给主合成的大小。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-04-08
          • 2021-12-08
          • 1970-01-01
          • 2011-05-29
          • 2014-01-12
          • 2017-04-06
          • 1970-01-01
          相关资源
          最近更新 更多