Rex Feral
Rex Feral

Reputation: 488

Setting a JFreeChart ChartComposite to a fixed size in SWT

I'm trying to add JFree step charts to a scrollable view in eclipse as a plugin, but the charts keep resizing (kinda randomly) each time I modify the view's dimensions.

I want to set them to a fixed size, however large or small the view might get (that's why I made it scrollable in the first place) so I tried something like this:

public void createPartControl(final Composite parent) {
    ScrolledComposite scrolled = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);        

    Composite comp = new Composite(scrolled, SWT.NONE);
    comp.setLayout(new FillLayout(SWT.VERTICAL));

    final JFreeChart chart = createChart();

    ChartComposite chartComposite1 = new ChartComposite(comp, SWT.NONE, chart, true);

    // set chart size attempt 

    chartComposite1.setSize(500, 200);
    chartComposite1.redraw();
    comp.redraw();
    scrolled.redraw();

    // ScrolledComposite stuff

    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));
        }
    });

But it doesn't do anything. The chart keeps auto resizing as I modify the view's size.

Any help?

Upvotes: 0

Views: 554

Answers (1)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 20985

Make sure that you undestand how layouts work in SWT. Understanding Layouts in SWT is a good start.

chartComposite1.setSize() is useless, it will be overwritten by the layout that was set in comp.setLayout( ... ).

... and forcing controls to redraw() doesn't help either as it doesn't update the location or size of controls.

As I suggested earlier, if you know the size of the charts, use a single columned GridLayout and control the size of the charts through GridData::widthHint and heightHint.

Upvotes: 1

Related Questions