2012-08-02 43 views
4

我有ScrolledComposite,它只允許垂直滾動。 (heighthint = 400)SWT - 在複合內包裝標籤

在這個ScrolledComposite中,我有另一個CompositeA(滾動的高度可能超過400)來存儲所有其他小部件。

我有一個非常長的標籤(啓用SWT.WRAP)。但不是包裝,它總是顯示在一條線上。我想這個標籤根據其父母的寬度包裹(CompositeA

我忘了補充一點CompositeA是2列「網格佈局」與makeColumnsEqualWidth = true

編輯: 這是我的代碼。

public void createPartControl(Composite parent) { 
    // TODO Auto-generated method stub 

    Display display = parent.getDisplay(); 

    toolkit = new FormToolkit(display); 
    form = toolkit.createForm(parent); 
    form.setText("ABC"); 

    Composite body = form.getBody(); 


    TableWrapLayout layout = new TableWrapLayout(); 
    layout.numColumns = 2; 
    body.setLayout(layout); 

    Label header1 = toolkit.createLabel(body, "ABC: "); 
    Font font = new Font(display, "Arial", 11, SWT.BOLD); 
    header1.setFont(font); 

    Label header2 = toolkit.createLabel(body, "XYZ", 
      SWT.WRAP); 
    font = new Font(display, "Arial", 11, SWT.NONE); 
    header2.setFont(font); 

    TableWrapData wd = new TableWrapData(TableWrapData.FILL_GRAB);  
    header2.setLayoutData(wd); 

    form.getBody().setBackground(
      form.getBody().getDisplay() 
        .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); 

    // Scrolled composite 
    ScrolledComposite sc = new ScrolledComposite(body, SWT.BORDER_SOLID 
      | SWT.V_SCROLL); 
    sc.setAlwaysShowScrollBars(true); 
    sc.setBackground(new Color(display, 50,255,155)); 


    wd = new TableWrapData(TableWrapData.FILL); 
    wd.heightHint = 360; 
    wd.colspan = 2; 
    wd.grabHorizontal = false; 
    sc.setLayoutData(wd); 

    sc.setLayout(new TableWrapLayout()); 

    Composite innerComposite = toolkit.createComposite(sc); 
    sc.setContent(innerComposite); 

    innerComposite.setLayout(new TableWrapLayout()); 
    innerComposite.setBackground(new Color(display, 255,50,50)); 

    Section section = toolkit.createSection(innerComposite, 
      Section.DESCRIPTION | Section.TITLE_BAR | Section.EXPANDED); 
    wd = new TableWrapData(TableWrapData.FILL); 
    wd.maxWidth = 600; // don't want to hardcode this value 

    section.setLayoutData(wd); 
    section.setText("Section"); 
    section.setDescription("A not so long description......................"); 

    // Composite for Section 
    Composite sectionClient = toolkit.createComposite(section); 
    layout = new TableWrapLayout(); 
    layout.numColumns = 2; 
    layout.makeColumnsEqualWidth = true; 
    sectionClient.setLayout(layout); 

    toolkit.createButton(sectionClient, "Button 1", SWT.RADIO); 

    Label rightDesc = toolkit 
      .createLabel(
        sectionClient, 
        "A very long long long long long long long long long long long long long long long long long long long long desc that needs wrapping", 
        SWT.WRAP); 
    font = new Font(display, "Arial", 10, SWT.ITALIC); 
    rightDesc.setFont(font); 
    wd = new TableWrapData(); 
    wd.rowspan = 2; 
    rightDesc.setLayoutData(wd); 

    Combo comboDropDown = new Combo(sectionClient, SWT.DROP_DOWN 
      | SWT.BORDER); 
    comboDropDown.setText("DDL"); 
    comboDropDown.add("1"); 
    comboDropDown.add("2"); 
    comboDropDown.add("3"); 

    Label lineBreak = toolkit.createSeparator(sectionClient, SWT.SEPARATOR 
      | SWT.HORIZONTAL); 
    wd = new TableWrapData(TableWrapData.FILL); 
    wd.colspan = 2; 
    lineBreak.setLayoutData(wd); 

    /***********************/ 

    toolkit.createButton(sectionClient, "Button 2", SWT.RADIO); 

    Label rightDesc2 = toolkit 
      .createLabel(
        sectionClient, 
        "A long long long long long long long long long long long long long long long long long long long long desc that needs wrapping", 
        SWT.WRAP); 
    font = new Font(display, "Arial", 10, SWT.ITALIC); 
    rightDesc2.setFont(font); 
    wd = new TableWrapData(TableWrapData.FILL); 
    wd.rowspan = 3; 
    rightDesc2.setLayoutData(wd); 

    toolkit.createLabel(sectionClient, "Desc", 
      SWT.WRAP); 
    toolkit.createText(sectionClient, "hello world", SWT.NONE); 

    section.setClient(sectionClient); 

    innerComposite.pack(); 

} 

如果您運行它,您可以看到一個綠色的滾動組合和一個紅色組合。我希望紅色合成寬度可以填充到滾動複合物的寬度,而不用硬編碼maxWidth = 600.

+1

我想我也有類似的問題,我解決它使用''爲horizo​​ntalAlignment' FILL'和標籤的'GridData'和包含其綜合TRUE;爲'grabExcessHorizo​​ntalSpace'。希望這可以幫助。 – Baldrick 2012-08-02 10:05:18

+0

我認爲當我調用'innerComposite.pack()'時,它會在單行中佈置標籤?但沒有'innerComposite.pack()'我將無法看到整個佈局。 – humansg 2012-08-03 02:33:49

回答

0

我可能記得這個錯誤,但我記得當文本改變時我不得不重新佈局組合。這使文本包裝。

+0

檢查ZoltánUjhelyi的解決方案以及以下內容:http://stackoverflow.com/questions/3499349/layout-issue-autogrow-label-swt – dbalakirev 2012-08-02 07:22:20

+0

文本沒有改變。只是當它第一次顯示時它已經是一個長文本了 – humansg 2012-08-02 07:38:47

+0

而且你在首先顯示它之後做一個佈局? – dbalakirev 2012-08-02 08:14:31

0

令人驚訝的是,理解TableWrapLayout經理不會指望水平滾動的複合材料。應該使用Form工具箱上的佈局。我剛剛做了一個代碼示例,可以採取以上做法。

public void createPartControl(Composite parent) { 
    // TODO Auto-generated method stub 

    Display display = parent.getDisplay(); 

    toolkit = new FormToolkit(display); 
    form = toolkit.createForm(parent); 
    form.setText("ABC"); 

    Composite body = form.getBody(); 

    TableWrapLayout layout = new TableWrapLayout(); 
    layout.numColumns = 2; 
    body.setLayout(layout); 

    Label header1 = toolkit.createLabel(body, "ABC: "); 
    Font font = new Font(display, "Arial", 11, SWT.BOLD); 
    header1.setFont(font); 

    Label header2 = toolkit.createLabel(body, "XYZ", SWT.WRAP); 
    font = new Font(display, "Arial", 11, SWT.NONE); 
    header2.setFont(font); 

    TableWrapData wd = new TableWrapData();  
    header2.setLayoutData(wd); 

    body.setBackground(body.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); 

    // Scrolled composite 
    Composite sc = toolkit.createComposite(body, SWT.BORDER_SOLID | SWT.V_SCROLL); 
    sc.setBackground(new Color(display, 50,255,155)); 
    layout = new TableWrapLayout();  
    sc.setLayout(layout); 

    wd = new TableWrapData(TableWrapData.FILL_GRAB); 
    wd.heightHint = 360; 
    wd.colspan = 2; 
    sc.setLayoutData(wd); 

    Composite innerComposite = toolkit.createComposite(sc); 

    layout = new TableWrapLayout(); 
    innerComposite.setLayout(layout); 
    innerComposite.setBackground(new Color(display, 255,50,50)); 

    wd = new TableWrapData(TableWrapData.FILL_GRAB); 
    innerComposite.setLayoutData(wd); 

    Section section = toolkit.createSection(innerComposite, 
      Section.DESCRIPTION | Section.TITLE_BAR | Section.TWISTIE | Section.EXPANDED); 

    section.setText("Section"); 
    section.setDescription("A not so long description......................"); 

    wd = new TableWrapData(TableWrapData.FILL_GRAB); 
    // wd.maxWidth = 600; // don't want to hardcode this value 
    section.setLayoutData(wd); 


    // Composite for Section 
    Composite sectionClient = toolkit.createComposite(section); 
    layout = new TableWrapLayout(); 
    layout.numColumns = 2; 
    // layout.makeColumnsEqualWidth = true; 
    sectionClient.setLayout(layout); 
    section.setClient(sectionClient); 

    wd = new TableWrapData(TableWrapData.FILL_GRAB); 
    sectionClient.setLayoutData(wd); 

    Label rightDesc = toolkit.createLabel(sectionClient, 
        "A very long long long long long long long long long long long long long long long long long long long long desc that needs wrapping", 
        SWT.WRAP); 
    font = new Font(display, "Arial", 10, SWT.ITALIC); 
    rightDesc.setFont(font); 
    wd = new TableWrapData(); 
    wd.colspan = 2; 
    rightDesc.setLayoutData(wd); 

    Combo comboDropDown = new Combo(sectionClient, SWT.DROP_DOWN | SWT.BORDER); 
    comboDropDown.setText("DDL"); 
    comboDropDown.add("1"); 
    comboDropDown.add("2"); 
    comboDropDown.add("3"); 

    Label lineBreak = toolkit.createSeparator(sectionClient, SWT.SEPARATOR | SWT.HORIZONTAL); 
    wd = new TableWrapData(TableWrapData.FILL_GRAB); 
    lineBreak.setLayoutData(wd); 
     // 
     // /***********************/ 
     // 
    Button button1 = toolkit.createButton(sectionClient, "Button 1", SWT.RADIO); 
     wd = new TableWrapData(); 
     wd.colspan = 2; 
     button1.setLayoutData(wd); 

    Button button2 = toolkit.createButton(sectionClient, "Button 2", SWT.RADIO); 
     wd = new TableWrapData(); 
     wd.colspan = 2; 
     button2.setLayoutData(wd); 

    Label rightDesc2 = toolkit.createLabel(sectionClient, 
        "A long long long long long long long long long long long long long long long long long long long long desc that needs wrapping", 
        SWT.WRAP); 
    font = new Font(display, "Arial", 10, SWT.ITALIC); 
    rightDesc2.setFont(font); 
    wd = new TableWrapData(); 
    wd.colspan = 2; 
    rightDesc2.setLayoutData(wd); 

    Label desc = toolkit.createLabel(sectionClient, "Desc:"); 
    Text hello = toolkit.createText(sectionClient, "hello world"); 
    wd = new TableWrapData(TableWrapData.FILL_GRAB); 
    hello.setLayoutData(wd); 
} 
1

嘗試在創建標籤時添加SWT.WRAP。它爲我工作。

Label label = new Label(parent, SWT.WRAP); 
4

我在這裏有我的佈局相同的問題,發現this有用的答案。

由於'這是一個沒有bug,它是一個功能'試着從評論#19 here這個答案。

我對我的標籤使用以下幾行代碼。

Label tip = new Label(shell, SWT.WRAP | SWT.BORDER | SWT.LEFT); 
final GridData data = new GridData(SWT.HORIZONTAL, SWT.TOP, true, false, 1, 1); 
tip.setLayoutData(data); 
tip.setText("Here stands my long, long and much more longer text..."); 
+1

'new GridData(SWT。HORIZONTAL,...)'看起來很奇怪,'HORIZONTAL'不是根據'GridData'文檔允許的常量之一。 – Lii 2016-09-20 12:37:40