javaeclipseswtcompositescrolledcomposite

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.

  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. Any help would be very appreciated.


Solution

  • You haven't told the ScrolledComposite how to manage the size. You must either call setSize or setMinSize. For this you probably want:

    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));