我有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.
我想我也有類似的問題,我解決它使用''爲horizontalAlignment' FILL'和標籤的'GridData'和包含其綜合TRUE;爲'grabExcessHorizontalSpace'。希望這可以幫助。 – Baldrick 2012-08-02 10:05:18
我認爲當我調用'innerComposite.pack()'時,它會在單行中佈置標籤?但沒有'innerComposite.pack()'我將無法看到整個佈局。 – humansg 2012-08-03 02:33:49