?
Solved

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

Posted on 2013-11-23
7
Medium Priority
?
1,221 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
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!

 
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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 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