Initial state/scrolling when Drawing on SWT Components [message #530010] |
Wed, 28 April 2010 06:45 |
Dominik Stadler Messages: 3 Registered: July 2009 |
Junior Member |
|
|
Hi,
I would like to create a SWT custom component which uses some drawing, like a border and possible other UI gimmicks.
The code example below is a cut down version and illustrates my approach, it uses a ScrolledComposite to display a list of "items", each of them surrounded by a custom-drawn border. However two things do not work:
* The initial display is without the drawings, resizing shows the border correctly
* After resizing, if you scroll down, the part that is scrolled into view does not contain the border
Has anybody a hint for me what is done incorrectly in my code or how I can get the two things right?
Thanks... Dominik.
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
/**
* Sample class that shows problems with initial drawing of the border and
* problems on scrolling
*
*/
public class DrawingIssueTest {
private static final int START_X = 5;
private static final int START_Y = 0;
private static final int ROUND_DELTA_X = 10;
private static final int ROUND_DELTA_Y = 5;
private static final int ROUNDING = 15;
public static void main(String[] args) throws Exception {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
// use a scrolled composite to scroll the items if they do not fit
final ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL);
Canvas main = new Canvas(scrolledComposite, SWT.NULL);
main.setLayout(new GridLayout(1, false));
// draw 20 components underneath each other
for(int i = 0;i < 20; i++) {
create(main, "headline");
}
scrolledComposite.setExpandVertical(true);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setContent(main);
scrolledComposite.setMinSize(main.computeSize(SWT.DEFAULT, SWT.DEFAULT));
shell.setSize(400, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* put a label and draw a rounded rectangle on the provided canvas.
*
* @param main
* @param title
*/
private static void create(final Canvas main, String title) {
final Canvas inner = new Canvas(main, SWT.NONE);
inner.setLayout(new GridLayout(2, false));
inner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
final Listener redrawListener = new Listener() {
@Override
public void handleEvent(Event event) {
if(!inner.isDisposed()) {
Rectangle rect = inner.getClientArea();
GC gc = new GC(inner);
gc.setLineWidth(1);
// clear the area before painting to avoid having artefacts from before
Color fg = gc.getForeground();
gc.setForeground(gc.getBackground());
gc.fillRectangle(rect);
gc.setForeground(fg);
gc.drawRoundRectangle(START_X,START_Y,rect.width-ROUND_DELTA_X,rect.height-ROUND_DELTA_Y, ROUNDING, ROUNDING);
gc.dispose();
}
}
};
// only need to draw on the background if we use the colored-version of the dialog
main.getShell().addListener(SWT.Resize, redrawListener);
main.getShell().addListener(SWT.Activate, redrawListener);
final Canvas innerText = new Canvas(inner, SWT.NONE);
innerText.setLayout(new GridLayout(1, false));
Label titleLabel = new Label(innerText, SWT.NONE);
titleLabel.setText(title);
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03222 seconds