Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to make gradient background in Java SWT for a toolbar?

Hi,
I am working on creating a GUI using Java SWT and in this GUI I would like to change the background color of a toolbar with a gradient color. However, I want this gradient to start after a certain point.

Please see the attachment to view the example.

This is my current code that creates my custom toolbar with the solid background color and i want (220,220,220) to be a gradient from one RGB to another from top to down.

    public CustomToolbar(Composite parent, int style, String toolBarCaption) {
        super(parent, style);
        setLayout(new GridLayout());
        Font font = new Font(parent.getDisplay(),"Arial",9, SWT.BOLD);
        Color lightgray = new Color (parent.getDisplay(), 220, 220, 220);
        toolBar = new ToolBar(this, SWT.FLAT); 
        caption = new Label(this, SWT.HORIZONTAL | SWT.CENTER | SWT.BOTTOM);
        caption.setText(toolBarCaption);
        caption.setFont(font);
        caption.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // center the caption 
        //caption.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        caption.setBackground(lightgray);
        //toolBar.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        toolBar.setBackground(lightgray);
        //setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        setBackground(lightgray);
    }

Open in new window



And just to give you an idea, and this is how I use this custom toolbar in another class:

        CustomToolbar custToolbar_Files = new CustomToolbar(compFILES, SWT.FLAT, "Files");
        
        item_selectFilesDirectories = new ToolItem(custToolbar_Files.getToolBar(), SWT.NONE);

Open in new window

gradient.png
Avatar of Tolgar
Tolgar

ASKER

Any ideas?
Avatar of mccarl
Can you post the full code for your CustomToolbar class?  (I don't think what you want to do is as simple as what we did for the CLabel in your other question, but you may be able to do it by providing a custom drawBackground() method in this CustomToolbar class)
Avatar of Tolgar

ASKER

This is my whole CustomToolbar class:

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ToolBar;


public class CustomToolbar extends Composite {
    private ToolBar toolBar;
    private Label caption;
    
    public CustomToolbar(Composite parent, int style, String toolBarCaption) {
        super(parent, style);
        setLayout(new GridLayout());
        Font font = new Font(parent.getDisplay(),"Arial",9, SWT.BOLD);
        Color lightgray = new Color (parent.getDisplay(), 220, 220, 220);
        toolBar = new ToolBar(this, SWT.FLAT); 
        caption = new Label(this, SWT.HORIZONTAL | SWT.CENTER | SWT.BOTTOM);
        caption.setText(toolBarCaption);
        caption.setFont(font);
        caption.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // center the caption 
        //caption.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        caption.setBackground(lightgray);
        //toolBar.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        toolBar.setBackground(lightgray);
        //setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        setBackground(lightgray);
    }

    public ToolBar getToolBar() {
        return toolBar;
    }

    public Label getCaption() {
        return caption;
    }

}

Open in new window


Thanks,
I've been playing around with this for a while and there just doesn't seem to be a way. The problem is that all the "standard" widgets (those in org.eclipse.swt.widgets) only support single colour backgrounds, and the "custom" widgets (those in org.eclipse.swt.custom) are the ones that support gradients, but there is not that many custom widgets there.

The only way I could see to do it would be to re-create an entire "custom" toolbar widget that supported the gradient background, but I am guessing that that might just be too much effort for you to worry about!?
Avatar of Tolgar

ASKER

I agree it's too much work but do you think we can work on it? I am not in a rush for this feature.

Will you be able help me?
Avatar of Tolgar

ASKER

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial