How can I give a background color to the toolbar in SWT

Posted on 2013-06-11
Last Modified: 2013-06-12
How can I give a color (let's say green) to the background of the toolbar in SWT?

Question by:Tolgar

Assisted Solution

TvMpt earned 100 total points
ID: 39239039
Display display = Display.getCurrent();
Color green= display.getSystemColor(SWT.COLOR_GREEN);
Color listBackground = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND);

Author Comment

ID: 39239106
Thank you for your reply. But it didn't work. I think I am doing something wrong.

This is the code that I have.

Can you please tell me what I am doing wrong?

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.layout.FillLayout;

public class Gui {
    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);

        Display display2 = Display.getCurrent();
        Color green= display2.getSystemColor(SWT.COLOR_GREEN);
        Color listBackground = display2.getSystemColor(SWT.COLOR_LIST_BACKGROUND); 
        shell.setLayout(new GridLayout());
        // SWT.BOTTOM to show at the bottom
        CTabFolder folder = new CTabFolder(shell, SWT.TOP);
        GridData data = new GridData(GridData.FILL, GridData.FILL, true, true, 5, 5);
        CTabItem cTabItem1 = new CTabItem(folder, SWT.NONE);
        CTabItem cTabItem2 = new CTabItem(folder, SWT.NONE);
        CTabItem cTabItem3 = new CTabItem(folder, SWT.NONE);
        Image image_Folder = new Image(display, "folder.png");
        Image image_shape_triangle = new Image(display, "shape_triangle.png");
        Image image_document = new Image(display, "document.png");
        Image image_delete = new Image(display, "delete.png");
        Image image_floppy_disk = new Image(display, "floppy_disk.png");
        Image image_navigate_right = new Image(display, "navigate_right.png");
     // -----------here starts TAB 1 --------------------  
        Composite compTab1 = new Composite(folder, SWT.NULL);
        GridLayout gridLayout1 = new GridLayout();
        compTab1.setLayoutData(new GridData());
        ToolBar toolBar1 = new ToolBar(compTab1, SWT.FLAT);
        ToolItem item1 = new ToolItem(toolBar1, SWT.PUSH);
        item1.setImage(image_Folder); item1.setText("Select Files/Directory"); item1.setToolTipText("Select the files or the directory you want to analyze");
        ToolItem item11 = new ToolItem(toolBar1, SWT.PUSH);
        item11.setImage(image_shape_triangle); item11.setText("Select Changelist"); item11.setToolTipText("Select the files in the changelist");
        ToolItem item111 = new ToolItem(toolBar1, SWT.PUSH);
        item111.setImage(image_document); item111.setText("Select Submit File"); item111.setToolTipText("Select the submit file");
        ToolItem item1111 = new ToolItem(toolBar1, SWT.PUSH);
        item1111.setImage(image_delete); item1111.setText("Clear List"); item1111.setToolTipText("Delete the list of all files below");
        new ToolItem(toolBar1, SWT.SEPARATOR);

        ToolItem item12 = new ToolItem(toolBar1, SWT.PUSH);
        item12.setImage(image_floppy_disk); item12.setText("Save"); item12.setToolTipText("Save the list of file below to a XML file for future reference");
        new ToolItem(toolBar1, SWT.SEPARATOR);
        ToolItem item13 = new ToolItem(toolBar1, SWT.PUSH);
        item13.setImage(image_navigate_right); item13.setText("Specify Checks"); item13.setToolTipText("Move to the next tab to select the list of checks");
        new ToolItem(toolBar1, SWT.SEPARATOR);
        Label lblLine1 = new Label(compTab1, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
        lblLine1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        Composite compButtons1 = new Composite(compTab1, SWT.NULL);
        FillLayout fillLayout1 = new FillLayout();
        fillLayout1.type = SWT.HORIZONTAL;
        //Button b1 = new Button(compButtons, SWT.PUSH); b1.setText("B1");
        //Button b1Wide = new Button(compButtons, SWT.PUSH); b1Wide.setText("Wide Button 1");
        //Button buton1 = new Button(compButtons, SWT.PUSH); buton1.setText("Button 1");
// -----------here starts TAB 2 --------------------      
        Image image_folder_document = new Image(display, "folder_document.png");
        Image image_selection_delete_2 = new Image(display, "selection_delete_2.png");

        Composite compTab2 = new Composite(folder, SWT.NULL);
        GridLayout gridLayout2 = new GridLayout();
        compTab2.setLayoutData(new GridData());
        ToolBar toolBar2 = new ToolBar(compTab2, SWT.FLAT);

        ToolItem item2 = new ToolItem(toolBar2, SWT.PUSH);
        item2.setImage(image_folder_document); item2.setText("Load saved check list"); item2.setToolTipText("Load the saved list of checks if you saved from previous runs");
        ToolItem item22 = new ToolItem(toolBar2, SWT.PUSH);
        item22.setImage(image_selection_delete_2); item22.setText("Unselect all"); item22.setToolTipText("Unselec all checks below");
        ToolItem item222 = new ToolItem(toolBar2, SWT.PUSH);
        item222.setImage(image_floppy_disk); item222.setText("Save selections"); item222.setToolTipText("Save the list of checks to a XML file for future reference");
        new ToolItem(toolBar2, SWT.SEPARATOR);
        ToolItem item23 = new ToolItem(toolBar2, SWT.PUSH);
        item23.setImage(image_navigate_right); item23.setText("Run checks"); item23.setToolTipText("Run the checks against the files you selected");
        new ToolItem(toolBar2, SWT.SEPARATOR);
        Label lblLine2 = new Label(compTab2, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
        lblLine2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        Composite compButtons2 = new Composite(compTab2, SWT.NULL);
        FillLayout fillLayout2 = new FillLayout();
        fillLayout2.type = SWT.HORIZONTAL;
        //Button b1 = new Button(compButtons, SWT.PUSH); b1.setText("B1");
        //Button b1Wide = new Button(compButtons, SWT.PUSH); b1Wide.setText("Wide Button 1");
        //Button buton1 = new Button(compButtons, SWT.PUSH); buton1.setText("Button 1");
     // -----------here starts TAB 3 --------------------      
        Image image_breakpoint = new Image(display, "breakpoint.png");
        Image image_redo = new Image(display, "redo.png");
        Image image_gecko = new Image(display, "gecko.png");
        Image image_navigate_left2 = new Image(display, "navigate_left2_orange.png");
        Image image_exit = new Image(display, "exit.png");
        Composite compTab3 = new Composite(folder, SWT.NULL);
        GridLayout gridLayout3 = new GridLayout();
        compTab3.setLayoutData(new GridData());
        ToolBar toolBar3 = new ToolBar(compTab3, SWT.FLAT);
        ToolItem item3 = new ToolItem(toolBar3, SWT.PUSH);
        item3.setImage(image_floppy_disk); item3.setText("Save"); item3.setToolTipText("Save the list of files and checks to a XML file for future reference");
        new ToolItem(toolBar3, SWT.SEPARATOR);
        ToolItem item33 = new ToolItem(toolBar3, SWT.PUSH);
        item33.setImage(image_breakpoint); item33.setText("Stop checks"); item33.setToolTipText("Stop running the checks");
        ToolItem item333 = new ToolItem(toolBar3, SWT.PUSH);
        item333.setImage(image_redo); item333.setText("Rerun failed"); item333.setToolTipText("Rerun failed checks");
        new ToolItem(toolBar3, SWT.SEPARATOR);
        ToolItem item3333 = new ToolItem(toolBar3, SWT.PUSH);
        item3333.setImage(image_floppy_disk); item3333.setText("Save results"); item3333.setToolTipText("Save the results to HTML or XML file");
        new ToolItem(toolBar3, SWT.SEPARATOR);
        ToolItem item33333 = new ToolItem(toolBar3, SWT.PUSH);
        item33333.setImage(image_gecko); item33333.setText("Save to geck"); item33333.setToolTipText("Save the results to a geck. Geck number will be asked when you click this button");
        new ToolItem(toolBar3, SWT.SEPARATOR);
        ToolItem item333333 = new ToolItem(toolBar3, SWT.PUSH);
        item333333.setImage(image_navigate_left2); item333333.setText("Start over"); item333333.setToolTipText("Go back to the first tab to start over");
        ToolItem item3333333 = new ToolItem(toolBar3, SWT.PUSH);
        item3333333.setImage(image_exit); item3333333.setText("configuration"); item3333333.setToolTipText("Quit the tool");
        new ToolItem(toolBar3, SWT.SEPARATOR);
        Composite compButtons3 = new Composite(compTab3, SWT.NULL);
        FillLayout fillLayout3 = new FillLayout();
        fillLayout2.type = SWT.HORIZONTAL;
        Label lblLine3 = new Label(compTab3, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
        lblLine3.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        //Button b1 = new Button(compButtons, SWT.PUSH); b1.setText("B1");
        //Button b1Wide = new Button(compButtons, SWT.PUSH); b1Wide.setText("Wide Button 1");
        //Button buton1 = new Button(compButtons, SWT.PUSH); buton1.setText("Button 1");
        shell.addShellListener(new ShellListener() {

            public void shellIconified(ShellEvent e) {
            public void shellDeiconified(ShellEvent e) {
            public void shellDeactivated(ShellEvent e) {
            public void shellClosed(ShellEvent e) {
                System.out.println("Client Area: " + shell.getClientArea());
            public void shellActivated(ShellEvent e) {
                int frameX = shell.getSize().x - shell.getClientArea().width;
                int frameY = shell.getSize().y - shell.getClientArea().height;
                shell.setSize(1200 + frameX, 1000 + frameY);
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {

Open in new window

LVL 16

Accepted Solution

Valeri earned 400 total points
ID: 39241243
right after creation(instantiation) of the tool bar put the following line:
the same will work for toolBar2 and toolBar3.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

825 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