Solved

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

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


Thanks,
0
Comment
Question by:Tolgar
3 Comments
 
LVL 9

Assisted Solution

by:TvMpt
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);
0
 

Author Comment

by:Tolgar
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;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;


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.setText("TOOL");
        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);
        folder.setLayoutData(data);
        CTabItem cTabItem1 = new CTabItem(folder, SWT.NONE);
        cTabItem1.setText("Files");
        CTabItem cTabItem2 = new CTabItem(folder, SWT.NONE);
        cTabItem2.setText("Checks");
        CTabItem cTabItem3 = new CTabItem(folder, SWT.NONE);
        cTabItem3.setText("Results");
       
        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.setLayout(gridLayout1);
        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");
       
        compButtons1.setLayout(fillLayout1);
       
        cTabItem1.setControl(compTab1);
// -----------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.setLayout(gridLayout2);
        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");
       
        compButtons2.setLayout(fillLayout2);
       
        cTabItem2.setControl(compTab2);
     // -----------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.setLayout(gridLayout3);
        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");
       
        compButtons3.setLayout(fillLayout3);
       
        cTabItem3.setControl(compTab3);
       
        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);
            }
        });
             
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
} 

Open in new window

0
 
LVL 16

Accepted Solution

by:
Valeri earned 400 total points
ID: 39241243
right after creation(instantiation) of the tool bar put the following line:
toolBar1.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_GREEN));
the same will work for toolBar2 and toolBar3.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changeXy challenge 13 57
topping1 challenge 7 51
create a gui in perl 3 45
Java / Linux and Regular Expressions 11 47
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

758 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

21 Experts available now in Live!

Get 1:1 Help Now