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

时间:2008-08-29 19:36:34

标签: java eclipse swt rcp

我做错了什么?

以下是我的代码的摘录:

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));
}

...但它剪辑了最后一个按钮: alt text

bigbrother82:那没用。

SCdF:我尝试了你的建议,现在滚动条已经消失了。我需要更多地研究它。

4 个答案:

答案 0 :(得分: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);
}

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

答案 1 :(得分: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();
}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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