防止SWT ScrolledComposite吃掉它的一部分孩子

Jer*_*emy 12 java eclipse rcp swt

我做错了什么?

以下是我的代码的摘录:

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));
}
Run Code Online (Sandbox Code Playgroud)

...但它剪辑了最后一个按钮: 替代文字

bigbrother82:那没用.

SCdF:我尝试了你的建议,现在滚动条消失了.我还需要做更多的工作.

qua*_*ial 12

这是使用时的常见障碍 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);
}
Run Code Online (Sandbox Code Playgroud)

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