• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

How to create toolstrip using SWT?

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
Tolgar
Asked:
Tolgar
1 Solution
 
ValeriCommented:
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
 
TolgarAuthor Commented:
Perfect solution!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now