How to open a folder by clicking an icon in the toolbar by using SWT?

Posted on 2013-06-11
Last Modified: 2013-06-12
I have a code that generates a UI using SWT.

In this UI, there is a toolbar and and one of the icons in the toolbar should open a folder when you click on it.

How can I set this icon to open a folder when you click on it?

I am sending the code below:

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);
        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

Question by:Tolgar
LVL 16

Accepted Solution

Valeri earned 500 total points
ID: 39241303
see the files attached to this question:
the first item of the first tab opens the folder. this is the code that opens the folder:

DirectoryDialog dlg = new DirectoryDialog(shell);
String dir =;

string "dir" returns the chosen file.

Author Closing Comment

ID: 39242353

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 55
hibernate example issues from command prompt 10 63
jboss wildfly 10.1 10 400
jsp error 6 42
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

685 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