2008-08-29 22 views
12

Tôi đã làm gì sai?Ngăn chặn SWT ScrolledComposite khỏi ăn một phần của nó là trẻ em

Dưới đây là một đoạn trích từ mã của tôi:

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

... nhưng nó clip nút cuối cùng: alt text

bigbrother82: Đó không làm việc.

SCdF: Tôi đã thử đề xuất của bạn và bây giờ thanh cuộn đã biến mất. Tôi cần phải làm việc thêm một chút về điều đó.

+0

Hey Jeremy, tôi chỉ mới nhận thấy rằng đề xuất của tôi đã không hoạt động. Bạn đã có may mắn với điều này chưa? – SCdF

+0

(btw, nếu bạn bình luận về câu trả lời của tôi thì nó sẽ xuất hiện trong phản hồi của tôi và tôi sẽ không bỏ lỡ nó) – SCdF

Trả lời

12

Đây là một trở ngại phổ biến khi sử dụng ScrolledComposite. Khi nó quá nhỏ đến mức thanh cuộn phải được hiển thị, điều khiển máy khách phải co lại theo chiều ngang để nhường chỗ cho thanh cuộn. Điều này có tác dụng phụ của việc làm cho một số nhãn bọc dòng, mà di chuyển các điều khiển sau xa hơn, làm tăng chiều cao tối thiểu cần thiết bởi các nội dung tổng hợp.

Bạn cần nghe các thay đổi chiều rộng trên tổng hợp nội dung (mParent), tính chiều cao tối thiểu một lần nữa cho chiều rộng nội dung mới và gọi setMinHeight() trên composite được cuộn với chiều cao mới.

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

Khi nghe các thay đổi về kích thước, lưu ý rằng chúng tôi bỏ qua bất kỳ sự kiện thay đổi kích thước nào có chiều rộng bằng nhau. Điều này là do thay đổi về chiều cao của nội dung không ảnh hưởng đến chiều cao tối thiểu tối thiểu của nội dung, miễn là chiều rộng là như nhau.

+0

Cảm ơn bạn, điều này đã giải quyết được vấn đề. – Jeremy

0

Bạn không cần phải tính lại kích thước của hộp cuộn sau bố cục?

2

Nếu tôi không nhầm bạn cần phải trao đổi các

mParent.layout(); 

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

nên bạn chỉ còn:

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

Hãy thử thiết lập .setMinWidth và .setMinHeight trên ScrolledComposite một khi bố trí đã được thực hiện, vượt qua nó kích thước của composite chính.

Các vấn đề liên quan