2008-08-29 47 views
12

我做錯了什麼?防止SWT ScrolledComposite吃掉它的一部分孩子

這裏是從我的代碼的摘錄:

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:我試了你的建議,現在滾動條消失了。我需要更多的工作。

+0

嘿傑里米,我只是注意到現在我的建議沒有奏效。你有這個運氣嗎? – SCdF 2008-09-29 05:32:36

+0

(順便說一句,如果你評論我的答案,那麼它會顯示在我的回覆中,我不會錯過) – SCdF 2008-09-29 05:33:08

回答

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

在偵聽大小的變化,注意,我們忽略任何調整事件,其中寬度保持不變。這是因爲內容高度的變化不會影響內容的最小高度,只要寬度相同即可。

0

您是否需要在佈局後重新計算scrollBox的大小?

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

試着在設置.setMinWidth和.setMinHeight一旦佈局完成,將其傳遞給主要組合的大小。