• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1261
  • Last Modified:

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
0
Tolgar
Asked:
Tolgar
  • 4
  • 3
1 Solution
 
TolgarAuthor Commented:
Any ideas?
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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)
0
 
TolgarAuthor Commented:
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,
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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!?
0
 
TolgarAuthor Commented:
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?
0
 
TolgarAuthor Commented:
Any ideas?
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I don't have any time at the moment (and this would take quite a bit of time, I think). But certainly, if you want to have a go and you have questions, feel free to open a new question and I (and others) can take a look.

My thinking though is that you will pretty much need to start from scratch, ie. you "custom" toolbar will just be a Canvas or similar, where you can do all the appropriate drawing, to a) draw the gradient background, and b) draw all the lines and text to make it appear to be a toolbar. You then need to make it respond to click events, etc so that it behaves like a toolbar.

The other alternative is perhaps a totally third party component, such as this may provide the styling that you need...

http://hexapixel.com/projects/ribbon
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now