简体   繁体   中英

Java SWT - Setting Canvas background color

I am using SWT Canvas for my project. My issue is that I am unable to set the background color for it. Here is my code. No matter what color I give for the background, I only get the light grey-ish default background color when I run my plugin. Can someone please help me with this?

Thanks!

@Override
public void createPartControl(Composite parent) {

    scParent = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    scParent.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
    scParent.setExpandHorizontal(true);
    scParent.setExpandVertical(true);

    // Create Canvas to hold the table
    tableCanvas = new Canvas(scParent, SWT.H_SCROLL | SWT.V_SCROLL);
        tableCanvas.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.heightHint = 116;
    gd.horizontalSpan = 3;
    tableCanvas.setLayoutData(gd);

Here is the workaround I did for the scrolled composite to work right. Earlier, the scrolling mechanism was also not working + I couldn't set the background. Here is the updated code:

@Override
public void createPartControl(Composite parent) {
    parent.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
    parent.setLayout(new FillLayout(SWT.HORIZONTAL));

    ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    scrolledComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
    scrolledComposite.setExpandHorizontal(true);
    scrolledComposite.setExpandVertical(true);
    scrolledComposite.setMinWidth(400);
    scrolledComposite.setMinHeight(400);

    Composite myViewParent = new Composite(scrolledComposite, SWT.NONE);
    myViewParent.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
    myViewParent.setLayout(null);

    Button btnNewButton = new Button(myViewParent, SWT.NONE);
    btnNewButton.setBounds(45, 237, 90, 30);
    btnNewButton.setText("New Button");

    scrolledComposite.setContent(myViewParent);
    scrolledComposite.setMinSize(myViewParent.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    parent.setSize(600, 300);

Like this for example to set the background color as blue.

tableCanvas.setBackground(tableCanvas.getDisplay().getSystemColor(SWT.COLOR_BLUE)); 

this article will be very instructive for you

http://eclipse.org/articles/Article-SWT-graphics/SWT_graphics.html#Canvas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM