防止SWT滚动复合从吃它的一部分的孩子 [英] Prevent SWT scrolledComposite from eating part of it's children

查看:88
本文介绍了防止SWT滚动复合从吃它的一部分的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是我的代码摘录:

 public $ createBoolean 
ScrolledComposite scrollBox = new ScrolledComposite(parent,SWT.V_SCROLL);
scrollBox.setExpandHorizo​​ntal(true);
mParent = new Composite(scrollBox,SWT.NONE);
scrollBox.setContent(mParent);
FormLayout layout = new FormLayout();
mParent.setLayout(layout);
//在这里添加一堆控件
mParent.layout();
mParent.setSize(mParent.computeSize(SWT.DEFAULT,SWT.DEFAULT,true));
}

但剪辑最后一个按钮:



bigbrother82:没有工作。



SCdF:我尝试过你的建议,现在滚动条已经消失了。我需要更多的工作。

解决方案

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



您需要收听对于内容复合体(mParent)上的宽度更改,再次给出新内容宽度时计算最小高度,并在带有新高度的滚动复合文件上调用setMinHeight()。

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

//在这里使用0确保水平滚动条不会出现。如果
//您希望水平条显示在某个阈值(例如100
//像素),则会发送该值。
scrollBox.setMinWidth(0);

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

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

//在这里添加一堆控件

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

//等到这里设置内容窗格方式调整大小的监听器将
//当滚动的复合材料首先调整大小mParent,然后
//计算最小高度并调用setMinHeight()
scrollBox.setContent(mParent);
}

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


What did I do wrong?

Here is an excerpt from my code:

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

but it clips the last button:

bigbrother82: That didn't work.

SCdF: I tried your suggestion, and now the scrollbars are gone. I need to work some more on that.

解决方案

This is a common hurdle when using ScrolledComposite. When the SC is gets so small that the scroll bar must be shown, the client control has to shrink horizontally to make room for the scroll bar. This has the side effect of making some labels wrap lines, which moved the following controls farther down, which increased the minimum height needed by the content composite.

You need to listen for width changes on the content composite (mParent), compute the minimum height again given the new content width, and call setMinHeight() on the scrolled composite with new height.

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

In listening for size changes, note that we ignore any resize events where the width stays the same. This is because changes in the height of the content do not affect the minimum height of the content, as long as the width is the same.

这篇关于防止SWT滚动复合从吃它的一部分的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆