Solved

How to create toolstrip using SWT?

Posted on 2013-06-06
2
422 Views
Last Modified: 2013-06-06
Hi,
I have a GUI mockup which can be seen in the attachment. I am basically trying to create a toolstrip. For each of the 3 tabs I will get different set of icons on the toolstrip.

I have a code that creates the tabs and some icons for each tab but it does not look the same as the one that I have in the attachment.

Can you please tell me what changes I should make in my code to have the similar look with the one in the attachment?


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.Shell;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
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");
               
        Composite compTab1 = new Composite(folder, SWT.NULL);      
        // Create the layout.
        RowLayout layout = new RowLayout();
        // Optionally set layout fields.
        layout.wrap = true;
        // Set the layout into the composite.
        // shell.setLayout(layout);
        // Create the children of the composite.
        FillLayout fillLayoutResults1 = new FillLayout();
        fillLayoutResults1.type = SWT.HORIZONTAL;
        new Button(compTab1, SWT.PUSH).setText("B1");
        new Button(compTab1, SWT.PUSH).setText("Wide Button 1");
        new Button(compTab1, SWT.PUSH).setText("Button 1");
               
        Text text = new Text(folder, SWT.BORDER);
        text.setText("You can type the list of files in this field");
        cTabItem1.setControl(text);
       
        compTab1.setLayout(layout);
        compTab1.setLayout(fillLayoutResults1);
        cTabItem1.setControl(compTab1);
       
        Composite compTab2 = new Composite(folder, SWT.NULL);
       
        FillLayout fillLayoutResults2 = new FillLayout();
        fillLayoutResults2.type = SWT.HORIZONTAL;
        Image image = new Image(display, "chart.png");      
        Button b2 = new Button(compTab2, SWT.PUSH); b2.setImage(image);
        Button b2Wide = new Button(compTab2, SWT.PUSH); b2Wide.setImage(image);
        Button buton2 = new Button(compTab2, SWT.PUSH); buton2.setImage(image); 
       
        compTab2.setLayout(fillLayoutResults2);
        cTabItem2.setControl(compTab2);
        
        Composite compTab3 = new Composite(folder, SWT.NULL);
        
        FillLayout fillLayoutResults3 = new FillLayout();
        fillLayoutResults3.type = SWT.HORIZONTAL;
        new Button(compTab3, SWT.PUSH).setText("B3");
        new Button(compTab3, SWT.PUSH).setText("Wide Button 3");
        new Button(compTab3, SWT.PUSH).setText("Button 3");
       
        compTab3.setLayout(fillLayoutResults3);
        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

A.png
B.png
C.png
0
Comment
Question by:Tolgar
2 Comments
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 39226582
check this, I added a menu only for tab 1, you can do the same for tab 2 and 3:

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 image = new Image(display, "chart.png");
       
        Composite compTab1 = new Composite(folder, SWT.NULL);
        RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
        compTab1.setLayout(rowLayout);
               
        ToolBar toolBar = new ToolBar(compTab1, SWT.FLAT);
       
               
        ToolItem item1 = new ToolItem(toolBar, SWT.PUSH);
        item1.setImage(image);
        new ToolItem(toolBar, SWT.SEPARATOR);
       
        ToolItem item2 = new ToolItem(toolBar, SWT.PUSH);
        item2.setImage(image);
        new ToolItem(toolBar, SWT.SEPARATOR);
       
        ToolItem item3 = new ToolItem(toolBar, SWT.PUSH);
        item3.setImage(image);
               
       
        Composite compButtons = new Composite(compTab1, SWT.NULL);
        FillLayout fillLayout = new FillLayout();
        fillLayout.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");
       
        compButtons.setLayout(fillLayout);
       
        cTabItem1.setControl(compTab1);
// -----------here starts TAB 2 --------------------      
        Composite compTab2 = new Composite(folder, SWT.NULL);
       
        FillLayout fillLayoutResults2 = new FillLayout();
        fillLayoutResults2.type = SWT.HORIZONTAL;
             
        Button b2 = new Button(compTab2, SWT.PUSH); b2.setImage(image);
        Button b2Wide = new Button(compTab2, SWT.PUSH); b2Wide.setImage(image);
        Button buton2 = new Button(compTab2, SWT.PUSH); buton2.setImage(image);
       
        compTab2.setLayout(fillLayoutResults2);
        cTabItem2.setControl(compTab2);
     // -----------here starts TAB 3 --------------------      
        Composite compTab3 = new Composite(folder, SWT.NULL);
       
        FillLayout fillLayoutResults3 = new FillLayout();
        fillLayoutResults3.type = SWT.HORIZONTAL;
        new Button(compTab3, SWT.PUSH).setText("B3");
        new Button(compTab3, SWT.PUSH).setText("Wide Button 3");
        new Button(compTab3, SWT.PUSH).setText("Button 3");
       
        compTab3.setLayout(fillLayoutResults3);
        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(600 + frameX, 500 + frameY);
            }
        });
             
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}
0
 

Author Closing Comment

by:Tolgar
ID: 39226709
Perfect solution!!
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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

708 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

18 Experts available now in Live!

Get 1:1 Help Now