eclipse rcp : custom drawing on composite which has a FillLayout not work
I am trying to ad开发者_如何学God a PaintListener to draw a top border on a Composite :
pageComposite.addPaintListener(new PaintListener(){
@Override
public void paintControl(PaintEvent e) {
e.gc.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
e.gc.drawLine(0, 0, pageComposite.getBounds().width, 0);
}
});
but if the pageComposite is set with a FillLayout, the border will not be drawn.
Composite c = new Composite(parent, SWT.NONE);
c.setLayout(new FillLayout());
new Label(c, SWT.NONE).setText("类方法列表页面,尚未实现");
return c;
the composite created in above code looks like this:
another composite has a FormLayout:final Composite c = new Composite(parent, SWT.NONE);
c.setLayout(new FormLayout());
Label l = new Label(c, SWT.NONE);
l.setText("角色用户机构列表页面,尚未实现");
FormData fd_content = new FormData();
fd_content.top = new FormAttachment(0, 10);
fd_content.left = new FormAttachment(0, 10);
l.setLayoutData(fd_content);
return c;
Did I miss anything ?
best regards! With the SWT org.eclipse.swt.layout.FillLayout
your paint event listener
is not getting called and that's why your border is not getting drawn.
The reason is when you use FillLayout
like this:
FillLayout fillLayout = new FillLayout();
composite.setLayout(fillLayout);
Then you are not setting any additional decorations like indentation
, margin
etc and hence an OS level update
is not called on your window.
If you want to make it work then set some additional layout data like marginHeight
. For example:
FillLayout fillLayout = new FillLayout();
fillLayout.marginHeight = 5;
composite.setLayout(fillLayout);
This is how it looks like after setting the above said layout decoration (note the top border):
>>Code
I have put some commented code. Try to remove the comment and see how it behaves.
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class FillLayoutTest
{
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout()); shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Color c = new Color(display, new RGB(127, 127, 127));
final Composite composite = new Composite(shell, SWT.NONE);
FillLayout fillLayout = new FillLayout();
fillLayout.marginHeight = 5;
composite.setLayout(fillLayout);
//composite.setLayout(new GridLayout()); composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
new Label(composite, SWT.BORDER).setText("Hello World!!");
new Label(composite, SWT.BORDER).setText("Hello World Version 2.0!!");
composite.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent e) {
e.gc.setForeground(c);
e.gc.drawLine(0, 0, composite.getParent().getBounds().width, 0);
//throw new RuntimeException();
}
});
shell.open();
shell.pack();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
if(c != null && !c.isDisposed())
c.dispose();
if(display != null && !display.isDisposed())
display.dispose();
}
}
精彩评论