如何在swt表单页面的部分显示滚动的复合内容
How display the scrolledcomposite content in section in swt formpage
这是我的示例代码,我在 class 中有一个名为 CommentPage 的部分,它从 FormPage 扩展而来,我在该部分中使用 scrolledComposite 来显示一些内容,但它确实有效,谁知道为什么?
public class CommentPage extends FormPage{
private FormToolkit toolkit;
public CommentPage (FormEditor editor, String id, String title) {
super(editor, id, title);
}
@Override
protected void createFormContent(IManagedForm managedForm) {
// TODO Auto-generated method stub
super.createFormContent(managedForm);
toolkit = managedForm.getToolkit();
form = managedForm.getForm();
toolkit.decorateFormHeading(form.getForm());
form.setText(currVersion.getcName()+currVersion.getvName());
Composite superContainer = form.getBody();
superContainer.setLayout(new GridLayout(2,false));
superContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
createAreas(superContainer);
}
private void createAreas(Composite superContainer) {
Composite left = toolkit.createComposite(superContainer, SWT.NONE);
Composite right = toolkit.createComposite(superContainer, SWT.NONE);
GridData leftGrid = new GridData(SWT.FILL,SWT.FILL,true,true);
left.setLayoutData(leftGrid);
left.setLayout(new GridLayout(1,true));
GridData rightGrid = new GridData(SWT.FILL,SWT.FILL,true,true);
right.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
right.setLayout(new GridLayout(1,true));
commentArea(right);
}
private void commentArea(Composite superContainer) {
Section section = toolkit.createSection(superContainer, Section.TITLE_BAR|Section.TWISTIE|Section.EXPANDED);
section.setText("Comment"); //$NON-NLS-1$
section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
section.setLayout(new FillLayout());
Composite container = toolkit.createComposite(section, SWT.NONE);
container.setLayout(new GridLayout(1, true));
section.setClient(container);
ScrolledComposite scrollContainer = new ScrolledComposite(container, SWT.V_SCROLL|SWT.BORDER);
scrollContainer.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
scrollContainer.setExpandVertical(true);
scrollContainer.setAlwaysShowScrollBars(true);
Color white = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
scrollContainer.setBackground(white);
Composite scrollInner = new Composite(scrollContainer, SWT.NONE);
scrollInner.setBackground(white);
scrollInner.setLayout(new GridLayout(1,false));
Label text = toolkit.createLabel(scrollInner, "test1test2test3");
text.setLayoutData(new GridData());
scrollContainer.setContent(scrollInner);
//scrollContainer.setMinSize(scrollInner.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
}
我的问题:
为什么没有显示方法 commentArea
中 scrolledContainer
中标签的文本 "test1test2test3"
?
在ScrolledComposite
上使用setExpandHorizontal
方法:
ScrolledComposite scrollContainer = new ScrolledComposite(container, SWT.V_SCROLL|SWT.BORDER);
scrollContainer.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
scrollContainer.setExpandVertical(true);
scrollContainer.setExpandHorizontal(true); // Add this line
将您的 ScrolledComposite
代码片段提取到易于测试的代码中:
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
// Your code below
final ScrolledComposite scrollContainer = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.BORDER);
scrollContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
scrollContainer.setExpandVertical(true);
scrollContainer.setExpandHorizontal(true); // Added
scrollContainer.setAlwaysShowScrollBars(true);
final Color white = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
scrollContainer.setBackground(white);
final Composite scrollInner = new Composite(scrollContainer, SWT.NONE);
scrollInner.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
scrollInner.setBackground(white);
scrollInner.setLayout(new GridLayout(1, false));
final Label text = new Label(scrollInner, SWT.NONE);
text.setText("test1test2test3");
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
scrollContainer.setContent(scrollInner);
// End your code
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
这是我的示例代码,我在 class 中有一个名为 CommentPage 的部分,它从 FormPage 扩展而来,我在该部分中使用 scrolledComposite 来显示一些内容,但它确实有效,谁知道为什么?
public class CommentPage extends FormPage{
private FormToolkit toolkit;
public CommentPage (FormEditor editor, String id, String title) {
super(editor, id, title);
}
@Override
protected void createFormContent(IManagedForm managedForm) {
// TODO Auto-generated method stub
super.createFormContent(managedForm);
toolkit = managedForm.getToolkit();
form = managedForm.getForm();
toolkit.decorateFormHeading(form.getForm());
form.setText(currVersion.getcName()+currVersion.getvName());
Composite superContainer = form.getBody();
superContainer.setLayout(new GridLayout(2,false));
superContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
createAreas(superContainer);
}
private void createAreas(Composite superContainer) {
Composite left = toolkit.createComposite(superContainer, SWT.NONE);
Composite right = toolkit.createComposite(superContainer, SWT.NONE);
GridData leftGrid = new GridData(SWT.FILL,SWT.FILL,true,true);
left.setLayoutData(leftGrid);
left.setLayout(new GridLayout(1,true));
GridData rightGrid = new GridData(SWT.FILL,SWT.FILL,true,true);
right.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
right.setLayout(new GridLayout(1,true));
commentArea(right);
}
private void commentArea(Composite superContainer) {
Section section = toolkit.createSection(superContainer, Section.TITLE_BAR|Section.TWISTIE|Section.EXPANDED);
section.setText("Comment"); //$NON-NLS-1$
section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
section.setLayout(new FillLayout());
Composite container = toolkit.createComposite(section, SWT.NONE);
container.setLayout(new GridLayout(1, true));
section.setClient(container);
ScrolledComposite scrollContainer = new ScrolledComposite(container, SWT.V_SCROLL|SWT.BORDER);
scrollContainer.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
scrollContainer.setExpandVertical(true);
scrollContainer.setAlwaysShowScrollBars(true);
Color white = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
scrollContainer.setBackground(white);
Composite scrollInner = new Composite(scrollContainer, SWT.NONE);
scrollInner.setBackground(white);
scrollInner.setLayout(new GridLayout(1,false));
Label text = toolkit.createLabel(scrollInner, "test1test2test3");
text.setLayoutData(new GridData());
scrollContainer.setContent(scrollInner);
//scrollContainer.setMinSize(scrollInner.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
}
我的问题:
为什么没有显示方法 commentArea
中 scrolledContainer
中标签的文本 "test1test2test3"
?
在ScrolledComposite
上使用setExpandHorizontal
方法:
ScrolledComposite scrollContainer = new ScrolledComposite(container, SWT.V_SCROLL|SWT.BORDER);
scrollContainer.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
scrollContainer.setExpandVertical(true);
scrollContainer.setExpandHorizontal(true); // Add this line
将您的 ScrolledComposite
代码片段提取到易于测试的代码中:
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
// Your code below
final ScrolledComposite scrollContainer = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.BORDER);
scrollContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
scrollContainer.setExpandVertical(true);
scrollContainer.setExpandHorizontal(true); // Added
scrollContainer.setAlwaysShowScrollBars(true);
final Color white = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
scrollContainer.setBackground(white);
final Composite scrollInner = new Composite(scrollContainer, SWT.NONE);
scrollInner.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
scrollInner.setBackground(white);
scrollInner.setLayout(new GridLayout(1, false));
final Label text = new Label(scrollInner, SWT.NONE);
text.setText("test1test2test3");
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
scrollContainer.setContent(scrollInner);
// End your code
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}