2011-10-17 70 views
6

我試了一下,到目前爲止:如何在Eclipse RCP應用程序中使視圖可滾動?

在的createPartControl:

ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL); 
     sc.setLayoutData(new GridData(GridData.FILL_BOTH)); 
     sc.setExpandVertical(true); 
     sc.setExpandHorizontal(true); 
     sc.setSize(ApplicationWorkbenchWindowAdvisor.WIDTH, ApplicationWorkbenchWindowAdvisor.HEIGHT); 
     final TabFolder tabFolder = new TabFolder(sc, SWT.TOP); 

但這不起作用。我的問題是,如果我調整我的程序窗口的滾動條不會出現在我的視圖。有任何想法嗎?

回答

7

Javadoc of ScrolledComposite描述了使用它的兩種方式,包括示例代碼。總結起來:

  1. 你要麼設置包含在你的ScrolledComposite上的控制/複合材料本身
  2. 或者你告訴你的ScrolledComposite的最小尺寸,以供其內容的控制/複合材料的尺寸。

目前,你既沒有做。您將尺寸設置爲ScrolledComposite,但除非您不使用佈局管理器,否則這沒有多大意義。在任何情況下,請參閱上面的鏈接瞭解官方示例代碼。

+3

非常感謝!現在我學會了在問愚蠢的問題之前總是檢查javadoc ... –

4

這是一小片的代碼爲我工作:

ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); 
    Composite composite = new Composite(sc, SWT.NONE); 
    sc.setContent(composite); 

    Label lblRelation = new Label(composite, SWT.NONE); 
    lblRelation.setBounds(10, 13, 74, 15); 
    lblRelation.setText("Label name:"); 
    composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 
+0

也適用於我。謝謝 – Michael

6
在Eclipse視圖

通常情況下,我希望我的控制,以獲取所有可用空間,並且只顯示滾動條,否則控制會低於縮水一個可用的大小。

其他答案是完全有效的,但我想添加一個完整的示例createPartControl方法(Eclipse e4)。

@PostConstruct 
public void createPartControl(Composite parent) { 
    ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL); 
    Composite composite = new Composite(sc, SWT.NONE); 
    sc.setContent(composite); 

    composite.setLayout(new GridLayout(2, false)); 

    Label label = new Label(composite, SWT.NONE); 
    label.setText("Foo"); 

    Text text = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI); 
    GridDataFactory.fillDefaults().grab(true, true).hint(400, 400).applyTo(text); 

    sc.setExpandHorizontal(true); 
    sc.setExpandVertical(true); 
    sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 
} 

注意.fillDefaults()意味着.align(SWT.FILL, SWT.FILL)

我通常使用這種模式,所以我創建了下面的小幫手方法:

public static ScrolledComposite createScrollable(Composite parent, Consumer<Composite> scrollableContentCreator) { 
    ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); 
    Composite composite = new Composite(sc, SWT.NONE); 
    sc.setContent(composite); 

    scrollableContentCreator.accept(composite); 

    sc.setExpandHorizontal(true); 
    sc.setExpandVertical(true); 
    sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 
    return sc; 
} 

由於Java的8個lambda表達式,你現在可以在一個非常緊湊的方式實現新的滾動複合材料:

createScrollable(container, composite -> { 
     composite.setLayout(new FillLayout()); 
     // fill composite with controls 
    }); 
+0

我得到的結果與調用'setMinSize'時相同或不相似,這看起來很奇怪......當我的編輯器以任何方式獲得比'GridData'中的大小提示更小的值時,出現滾動條。也許這與它是一個多頁面的Forms編輯器有關... – Lii

相關問題