Solved

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

Posted on 2013-11-23
7
1,124 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
  • 4
  • 3
7 Comments
 

Author Comment

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

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 35

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 35

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now