0

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.

Justin
  • 39
  • 7

1 Answers1

2

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));
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • That worked, but when I make my window smaller, the canvas and labels will begin to hide but the scrollbars do not appear. The only time they appear is when I make the window so small and the amount that I can scroll to the first item in my legend and the others are hidden. Is there a way to have the composite noticed that a canvas or a label went below the threshold and display scrollbars without have to hardcode specific size of the shell? – Justin Jul 12 '17 at 15:19
  • The min size is the only thing you tell ScrolledComposite about. Using a layout such as GridLayout for the composite gives you more control over the size calculated by computeSize. – greg-449 Jul 12 '17 at 15:27