Solved

How to give to each group a name in the toolbar and draw a line underneath the toolbar in SWT?

Posted on 2013-06-06
3
315 Views
Last Modified: 2013-06-11
Follow up question based on the following question:

http://www.experts-exchange.com/Programming/Languages/Java/Q_28149674.html


Now, I have the icons and separators between them.

How can I create a line under the toolbar and give name to each group as in the mockup that I attached in the original question?

This is the final code that I have:

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.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
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.widgets.Button;
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);
               
        shell.setText("MY 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,
                20, 10);
        folder.setLayoutData(data);
        CTabItem cTabItem1 = new CTabItem(folder, SWT.NONE);
        cTabItem1.setText("A");
        CTabItem cTabItem2 = new CTabItem(folder, SWT.NONE);
        cTabItem2.setText("B");
        CTabItem cTabItem3 = new CTabItem(folder, SWT.NONE);
        cTabItem3.setText("C");
       
        Image image1 = new Image(display, "chart.png");
       
     // -----------here starts TAB 1 --------------------  
        
        Composite compTab1 = new Composite(folder, SWT.NULL);
        RowLayout rowLayout1 = new RowLayout(SWT.VERTICAL);
        compTab1.setLayout(rowLayout1);
               
        ToolBar toolBar1 = new ToolBar(compTab1, SWT.FLAT);
       
               
        ToolItem item1 = new ToolItem(toolBar1, SWT.PUSH);
        item1.setImage(image1);
        ToolItem item11 = new ToolItem(toolBar1, SWT.PUSH);
        item11.setImage(image1);
        ToolItem item111 = new ToolItem(toolBar1, SWT.PUSH);
        item111.setImage(image1);
        ToolItem item1111 = new ToolItem(toolBar1, SWT.PUSH);
        item1111.setImage(image1);
        new ToolItem(toolBar1, SWT.SEPARATOR);
       
        ToolItem item12 = new ToolItem(toolBar1, SWT.PUSH);
        item12.setImage(image1);
        new ToolItem(toolBar1, SWT.SEPARATOR);
       
        ToolItem item13 = new ToolItem(toolBar1, SWT.PUSH);
        item13.setImage(image1);
        new ToolItem(toolBar1, SWT.SEPARATOR);       
       
        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 image2 = new Image(display, "chart2.png");
        
        Composite compTab2 = new Composite(folder, SWT.NULL);
        RowLayout rowLayout2 = new RowLayout(SWT.VERTICAL);
        compTab2.setLayout(rowLayout2);
               
        ToolBar toolBar2 = new ToolBar(compTab2, SWT.FLAT);
       
               
        ToolItem item2 = new ToolItem(toolBar2, SWT.PUSH);
        item2.setImage(image2);
        ToolItem item22 = new ToolItem(toolBar2, SWT.PUSH);
        item22.setImage(image2);
        ToolItem item222 = new ToolItem(toolBar2, SWT.PUSH);
        item222.setImage(image2);
        new ToolItem(toolBar2, SWT.SEPARATOR);
              
        ToolItem item23 = new ToolItem(toolBar2, SWT.PUSH);
        item23.setImage(image2);
        new ToolItem(toolBar2, SWT.SEPARATOR);
               
       
        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 image3 = new Image(display, "chart3.png");
        
        Composite compTab3 = new Composite(folder, SWT.NULL);
        RowLayout rowLayout3 = new RowLayout(SWT.VERTICAL);
        compTab3.setLayout(rowLayout3);
               
        ToolBar toolBar3 = new ToolBar(compTab3, SWT.FLAT);
       
               
        ToolItem item3 = new ToolItem(toolBar3, SWT.PUSH);
        item3.setImage(image3);
        new ToolItem(toolBar3, SWT.SEPARATOR);
       
        ToolItem item33 = new ToolItem(toolBar3, SWT.PUSH);
        item33.setImage(image3);
        ToolItem item333 = new ToolItem(toolBar3, SWT.PUSH);
        item333.setImage(image3);
        new ToolItem(toolBar3, SWT.SEPARATOR);
       
        ToolItem item3333 = new ToolItem(toolBar3, SWT.PUSH);
        item3333.setImage(image3);
        new ToolItem(toolBar3, SWT.SEPARATOR);
        
        ToolItem item33333 = new ToolItem(toolBar3, SWT.PUSH);
        item33333.setImage(image3);
        new ToolItem(toolBar3, SWT.SEPARATOR);
        
        ToolItem item333333 = new ToolItem(toolBar3, SWT.PUSH);
        item333333.setImage(image3);
        ToolItem item3333333 = new ToolItem(toolBar3, SWT.PUSH);
        item3333333.setImage(image3);
        new ToolItem(toolBar3, SWT.SEPARATOR);
               
       
        Composite compButtons3 = new Composite(compTab3, SWT.NULL);
        FillLayout fillLayout3 = 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");
       
        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
Comment
Question by:Tolgar
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 39237485
1. about the lines. you can create horizontal and vertical lines in this way:
Label line = new Label(theParentComponent, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
2. you can give name and tooltip text to the components from toolbar by using "setText" and "setToolTipText" methods. see the code:

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);
               
        shell.setText("MY 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("A");
        CTabItem cTabItem2 = new CTabItem(folder, SWT.NONE);
        cTabItem2.setText("B");
        CTabItem cTabItem3 = new CTabItem(folder, SWT.NONE);
        cTabItem3.setText("C");
       
        Image image1 = new Image(display, "chart1.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(image1); item1.setText("configuration"); item1.setToolTipText("tool tip for this item");
        ToolItem item11 = new ToolItem(toolBar1, SWT.PUSH);
        item11.setImage(image1); item11.setText("configuration"); item11.setToolTipText("tool tip for this item");
        ToolItem item111 = new ToolItem(toolBar1, SWT.PUSH);
        item111.setImage(image1); item111.setText("configuration"); item111.setToolTipText("tool tip for this item");
        ToolItem item1111 = new ToolItem(toolBar1, SWT.PUSH);
        item1111.setImage(image1); item1111.setText("configuration"); item1111.setToolTipText("tool tip for this item");
        new ToolItem(toolBar1, SWT.SEPARATOR);
       
        ToolItem item12 = new ToolItem(toolBar1, SWT.PUSH);
        item12.setImage(image1); item12.setText("configuration"); item12.setToolTipText("tool tip for this item");
        new ToolItem(toolBar1, SWT.SEPARATOR);
       
        ToolItem item13 = new ToolItem(toolBar1, SWT.PUSH);
        item13.setImage(image1); item13.setText("configuration"); item13.setToolTipText("tool tip for this item");
        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 image2 = new Image(display, "chart2.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(image2); item2.setText("configuration"); item2.setToolTipText("tool tip for this item");
        ToolItem item22 = new ToolItem(toolBar2, SWT.PUSH);
        item22.setImage(image2); item22.setText("configuration"); item22.setToolTipText("tool tip for this item");
        ToolItem item222 = new ToolItem(toolBar2, SWT.PUSH);
        item222.setImage(image2); item222.setText("configuration"); item222.setToolTipText("tool tip for this item");
        new ToolItem(toolBar2, SWT.SEPARATOR);
             
        ToolItem item23 = new ToolItem(toolBar2, SWT.PUSH);
        item23.setImage(image2); item23.setText("configuration"); item23.setToolTipText("tool tip for this item");
        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 image3 = new Image(display, "chart3.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(image3); item3.setText("configuration"); item3.setToolTipText("tool tip for this item");
        new ToolItem(toolBar3, SWT.SEPARATOR);
       
        ToolItem item33 = new ToolItem(toolBar3, SWT.PUSH);
        item33.setImage(image3); item33.setText("configuration"); item33.setToolTipText("tool tip for this item");
        ToolItem item333 = new ToolItem(toolBar3, SWT.PUSH);
        item333.setImage(image3); item333.setText("configuration"); item333.setToolTipText("tool tip for this item");
        new ToolItem(toolBar3, SWT.SEPARATOR);
       
        ToolItem item3333 = new ToolItem(toolBar3, SWT.PUSH);
        item3333.setImage(image3); item3333.setText("configuration"); item3333.setToolTipText("tool tip for this item");
        new ToolItem(toolBar3, SWT.SEPARATOR);
       
        ToolItem item33333 = new ToolItem(toolBar3, SWT.PUSH);
        item33333.setImage(image3); item33333.setText("configuration"); item33333.setToolTipText("tool tip for this item");
        new ToolItem(toolBar3, SWT.SEPARATOR);
       
        ToolItem item333333 = new ToolItem(toolBar3, SWT.PUSH);
        item333333.setImage(image3); item333333.setText("configuration"); item333333.setToolTipText("tool tip for this item");
        ToolItem item3333333 = new ToolItem(toolBar3, SWT.PUSH);
        item3333333.setImage(image3); item3333333.setText("configuration"); item3333333.setToolTipText("tool tip for this item");
        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();
            }
        }
    }
}
0
 

Author Comment

by:Tolgar
ID: 39238525
Perfect! Thank you so much!
0
 

Author Comment

by:Tolgar
ID: 39238613
I had one more question actually. How can I give name to each group of icons. Let's say there are two icons named stop and rerun. I can give name to the icons as you mentioned but how am I going to give a name to the group of these icons.

For example, in Word, the font control icons are labeled as "Font" in the toolstrip. Or paragraph modification icons are named as "Paragraph". How can I do it in SWT?

And how can I give color to the toolbar. Let's say I want to make the background of the toolbar light green. How can I do it in SWT?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
servlet web applications   metadata-complete="true" or false 3 110
jsp error 6 25
Running JavaFX on the Raspberry Pi 27 40
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

856 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