[英]How to get contents to show up inside a ScrolledComposite
I am creating a legend view and inside the shell is supposed to have a rectangle followed by a label describing the color. 我正在创建图例视图,并且外壳内部应该有一个矩形,后跟一个描述颜色的标签。 I was able to get the view to work using just a normal composite but the legend continues beyond the screen and no way of see it without making the window larger. 我仅使用普通的合成就可以使视图工作,但是图例继续超出屏幕,并且如果不增大窗口就无法查看。 I am trying to use a scrolledComposite view for my shell but when I execute the program, nothing appears. 我正在尝试对我的外壳使用scrolledComposite视图,但是当我执行该程序时,什么也没出现。
public void createPartControl(Composite parent)
{
display = parent.getDisplay();
parent.setLayout(new FillLayout());
sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
LegendView.composite = new Composite(sc, SWT.NONE);
RowLayout layout = new RowLayout();
layout.wrap = true;
layout.spacing = 5;
composite.setLayout(layout);
}
public static void addRectangle(String legendMessage)
{
final String propId = legendMessage;
final String[] s = propId.split(",");
if (display != null)
{
display.syncExec(new Runnable()
{
@Override
public void run()
{
// Creating the color using the RBG values
final Color color =
new Color(display, Integer.parseInt(s[0]), Integer.parseInt(s[1]), Integer.parseInt(s[2]));
// Creating a canvas for which the rectangle can be drawn on
Canvas canvas = new Canvas(composite, SWT.NONE);
// Maybe set the bounds of the canvas
canvas.addPaintListener(new PaintListener()
{
public void paintControl(PaintEvent e)
{
e.gc.drawRectangle(1, 1, 50, 60);
e.gc.setBackground(color);
e.gc.fillRectangle(2, 2, 49, 59);
}
});
// Disposing the color after it has been used
canvas.addDisposeListener(new DisposeListener()
{
public void widgetDisposed(DisposeEvent e)
{
color.dispose();
}
});
// Creating a label and setting the font
Label label = new Label(composite, SWT.NULL);
Font boldFont = new Font( label.getDisplay(), new FontData( "Arial", 12, SWT.BOLD ) );
label.setFont( boldFont );
label.setText(s[3]);
composite.redraw();
composite.layout(true);
sc.setContent(composite);
}
});
}
}
I am calling add rectangle in a different class. 我在另一个类中调用添加矩形。 I am fairly new at using SWT and after looking at examples and reading the docs for scrolled Composite, this is what I interpreted it as. 我对使用SWT相当陌生,在查看示例并阅读了滚动式Composite的文档后,这就是我的解释。 Any help would be very appreciated. 任何帮助将不胜感激。
You haven't told the ScrolledComposite
how to manage the size. 您尚未告诉ScrolledComposite
如何管理大小。 You must either call setSize
or setMinSize
. 您必须调用setSize
或setMinSize
。 For this you probably want: 为此,您可能需要:
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.