Solved

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

Posted on 2013-11-23
7
1,206 Views
Last Modified: 2013-12-16
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
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

Author Comment

by:Tolgar
ID: 39673195
Any ideas?
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39676308
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
 

Author Comment

by:Tolgar
ID: 39676376
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 36

Expert Comment

by:mccarl
ID: 39684683
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
 

Author Comment

by:Tolgar
ID: 39686163
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
 

Author Comment

by:Tolgar
ID: 39691574
Any ideas?
0
 
LVL 36

Accepted Solution

by:
mccarl earned 500 total points
ID: 39691650
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
This is an explanation of a simple data model to help parse a JSON feed
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question