Setting a JFreeChart ChartComposite to a fixed size in SWT - java

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?

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.

Related

java composite with an scrolling image

I am newbie with swt and I have a Dialog that contains an image but if the image is too big is not possible to see the complete image. It is cut. So I decided to create a Dialog with scrolling. I have done but the scroll doesn't work. It appears and you can clic over and move it but I can't move over the image. I have tried several things but none of them work :(
protected Control createDialogArea(Composite parent) {
//getShell().setText(Messages.labelTitle);
//setTitleImage(image); // Image to be displayed in your Dialog
//return parent;
Composite container = new Composite(getShell(), SWT.H_SCROLL | SWT.V_SCROLL);
container.setLayoutData(new GridData(GridData.FILL_BOTH));
container.setLayout(new GridLayout());
Label preview = new Label(container, SWT.NONE);
GridData layoutData = new GridData(GridData.FILL_BOTH);
preview.setLayoutData(layoutData);
preview.setImage(image);
return parent;
}

Composite position and size - SWT

I need your help... I attached an image, I want that the right grid appears at the bottom, below of Estadisticas1, Estadisticas2, Estadisticas3, Estadisticas4
I tried a lot of ways, with GridData, FormLayout and no way!
Also, I tried with setSize and setBounds and in no cases the size or position change, I donĀ“t know why!
There are many ways to do this depending on exactly what you want which you haven't really specified. For example:
public void createPartControl(final Composite parent)
{
Composite body = new Composite(parent, SWT.NONE);
body.setLayout(new FillLayout(SWT.VERTICAL));
Composite topArea = new Composite(body, SWT.BORDER);
// TODO add your 'EstadisticasX' controls to 'topArea'
Composite bottomArea = new Composite(body, SWT.BORDER);
// TODO add bottom grid to 'bottomArea'
}

Eclipse SWT: How to disable scrolledcomposite scrolling on mousewheel

so I have
A Part with : (I will spare you the layout details, it's just to show my setup)
sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
drawingPane = new Composite(sc, SWT.NONE);
drawingPane.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
sc.setContent(drawingPane);
paintCanvas = new Canvas(drawingPane, SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND);
When I add then a MouseWheelListener to the Canvas, each Time the event occurs, the vertical Scrollbar of the ScrolledComposite is also reacting to it.
Which is kind of annoying. I tried the Solution of this Question. But the Thing is that the Canvas is the Source of the Event, which is right and I need it to be.
The question is more how can I prohibit the MouseEvent to get passed on to the ScrolledComposite?
I also tried to disable the Scrollbar, but this wasn't practical as I have found no place in the Code to re-enable it afterwards. As it seems that the scrolling gets triggered after anything else(like the redraw of my canvas for example).
A bit more Code would be helpful. Anyway, you could try the following:
paintCanvas.addListener(SWT.MouseVerticalWheel, new Listener() {
#Override
public void handleEvent(Event event) {
event.doit = false;
}
});
But I don't think that this will work, since MouseEvents aren't TraverseEvents. I couldn't test it for myself with your code snippet.

SWT drag and drop custom Control displayed next to cursor

How do i display some custom Controls or widgets next to cursor when dragging in SWT?
Like Button or Tree or Table not Image.
If it is not possible - how do i render such a Button into Image?
I've found the way of rendering arbitrary Control (widget) into Image using GC (canvas) class. Here is how:
dragSource.addDragListener(new DragSourceListener() {
#Override
public void dragStart(DragSourceEvent dragSourceEvent) {
// getting control widget - Composite in this case
Composite composite = (Composite) ((DragSource) dragSourceEvent.getSource()).getControl();
// Getting dimensions of this widget
Point compositeSize = composite.getSize();
// creating new GC
GC gc = new GC(composite);
// Creating new Image
Image image = new Image(Display.getCurrent(),compositeSize.x,compositeSize.y);
// Rendering widget to image
gc.copyArea(image,0,0);
// Setting widget to DnD image
dragSourceEvent.image = image;
}
... other overriden methods ...
}
You could use an undecorated Shell that follows the mouse position, and in that Shell have the widget(s) you want to display.

Canvas too small to display Image in SWT

I'm trying to use a Canvas to display an animated gif. I have the animation working, but the canvas is too small to see the entire image, and I cannot get it to resize.
I can't even get it to be the right size for the first frame, so the animation/threading code is being omitted. My code looks like:
Composite comp = new Composite(parent, SWT.None);
comp.setLayout(new GridLayout(1, false));
final ImageLoader loader = new ImageLoader();
loader.load(getClass().getClassLoader().getResourceAsStream("MYFILE.gif"));
final Canvas canvas = new Canvas(comp, SWT.None);
final Image image = new Image(Display.getCurrent(), loader.data[0]);
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawImage(image, 0, 0);
}
});
The canvas area ends up being a tiny box of around 30x30, regardless of how big MYFILE.gif is. What the hell?
Anything I try to use to set the canvas size (setSize, setBounds, etc) does nothing.
Try using setPreferredSize. The layout manager will call setSize and/or setBounds and clobber the values you have set, but it will try to respect preferred size if it can. You can also use setMaximumSize and setMinimumSize to control how the canvas will resize if the enclosing component is resized.
EDIT: For SWT you could try this:
GridData gridData = new GridData();
gridData.widthHint = imageWidth;
gridData.heightHint = imageHeight;
canvas.setLayoutData(gridData);
I'm not an SWT expert, but the SWT documentation seems helpful.

Categories