简体   繁体   English

如何使内容显示在ScrolledComposite中

[英]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 . 您必须调用setSizesetMinSize 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.

相关问题 SWT如何打印scrolledComposite的内容? - SWT How to print contents of a scrolledComposite? 如何在ScrolledComposite中显示多行文本? - How to display text with many lines inside a ScrolledComposite? 如何在ScrolledComposite内获取包含FlowLayout的Composite使其垂直而不是水平增长? - How do I get a Composite containing FlowLayout inside a ScrolledComposite to grow vertical and not horizontal? ScrolledComposite中的SWT GridLayout搞乱了组件的大小 - SWT GridLayout inside ScrolledComposite messing up the size of the components TabItem 内的 ScrolledComposite - ScrolledComposite inside TabItem 禁用ScrolledComposite内部的CheckboxTableViewer时如何启用滚动? - How to enable scroll when CheckboxTableViewer inside a ScrolledComposite is disabled? 当滚动条未显示时,如何摆脱SWT ScrolledComposite中的浪费空间 - How to get rid of wasted space in SWT ScrolledComposite when the scrollbars are not shown 使用GridLayout时如何使滚动条在ScrolledComposite中工作? - How to get the scrollbars to work in a ScrolledComposite when using GridLayout? 如何从另一个类中获取ArrayList以显示在JComboBox中? - How to get ArrayList from another class to show up inside a JComboBox? 如何填写ScrolledComposite? - How to fill a ScrolledComposite?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM