Search code examples
javaswtjfreechart

How to add composites to a scrolled container composite in SWT


I'm trying to create a scrollable container window to which I can add JFreeChart step charts, one under another, based on some values from different files but I am encountering a couple of problems. One of them is the fact that I'm not able to put the ChartComposite objects in the container, once I run the program it shows an empty window. I'm surely doing something wrong but I honestly have no idea how it should be.

Here's the code on how I try to put some charts into a container.

public void createPartControl(Composite parent) {

    final ScrolledComposite scrolled = new ScrolledComposite(parent, SWT.V_SCROLL);
    Composite comp = new Composite(scrolled, SWT.NONE);
    scrolled.setLayout(new FillLayout(SWT.VERTICAL));

    final JFreeChart chart = createChart();
    final JFreeChart chart1 = createChart();
    final JFreeChart chart2 = createChart();
    final JFreeChart chart3 = createChart();

    new ChartComposite(comp, SWT.NONE, chart, true);
    new ChartComposite(comp, SWT.NONE, chart1, true);
    new ChartComposite(comp, SWT.NONE, chart2, true);
    new ChartComposite(comp, SWT.NONE, chart3, true);

    comp.setLayout(new FillLayout(SWT.VERTICAL));

    scrolled.setContent(comp);

    scrolled.setExpandVertical(true);
    scrolled.setExpandHorizontal(true);

    scrolled.setAlwaysShowScrollBars(true);

    scrolled.addControlListener(new ControlAdapter() {
        public void controlResized(ControlEvent e) {
            org.eclipse.swt.graphics.Rectangle r = scrolled.getClientArea();
            scrolled.setMinSize(parent.computeSize(r.width, SWT.DEFAULT));
        }
    });

}

Any help or a link to some great tutorials on how to do stuff like this would be welcome.

EDIT: Did try a little with ScrolledComposite, modified the code accordingly, but it expands the charts to fit the whole view and is by no means scrollable.


Solution

  • Here is a working example for you to adapt:

    public static void main( String[] args ) {
      Display display = new Display();
      Shell shell = new Shell( display );
      shell.setLayout( new FillLayout() );
      ScrolledComposite scrolled = new ScrolledComposite( shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL );
      scrolled.setExpandVertical( true );
      scrolled.setExpandHorizontal( true );
      scrolled.setAlwaysShowScrollBars( true );
      Composite composite = new Composite( scrolled, SWT.NONE );
      composite.setLayout( new FillLayout( SWT.VERTICAL ) );
      for( int i = 0; i < 6; i++ ) {
        Composite item = new Composite( composite, SWT.NONE );
        item.setBackground( item.getDisplay().getSystemColor( SWT.COLOR_BLACK + i ) );
      }
      scrolled.setContent( composite );
      scrolled.setMinSize( composite.computeSize( SWT.DEFAULT, SWT.DEFAULT ) );
      scrolled.addControlListener( new ControlAdapter() {
        public void controlResized( ControlEvent event ) {
          Rectangle clientArea = scrolled.getClientArea();
          scrolled.setMinSize( composite.computeSize( clientArea.width, SWT.DEFAULT ) );
        }
      } );
      shell.setSize( 300, 300 );
      shell.open();
      while( !shell.isDisposed() ) {
        if( !display.readAndDispatch() )
          display.sleep();
      }
      display.dispose();
    }
    

    The items represent your CharComposites. If that doesn't show the desired result, you need to revise the ChartComposite::computeSize() implementation, or use a single columned GridLayout and control the size of the charts through GridData::widthHint and heightHint.