Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to add a listener to an icon to change the tab to the next one in SWT?

Hi,
I have the following code to create a UI. How can I add a listener to one of the icons on the toolbar so that it will change the tab to the next one tab?



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("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

Avatar of Valeri
Valeri
Flag of Bulgaria image

see again the files attached to this question:
https://www.experts-exchange.com/questions/28154257/How-to-give-a-name-to-a-toolbar-menu-in-SWT.html

The second item of the first tab changes the tab to the second tab. CTabFolder has method "setSelection(index)" where "index" is the index of the tab you want to switch to. "index" is zero based. In this case it switches to the second tab, which has index "1".
Avatar of Tolgar
Tolgar

ASKER

I tried it but when I click on the icon, it closes the window rather than switching to the next tab. Why do you think so?
Avatar of Tolgar

ASKER

And this is the error that I see on the console:

Exception in thread "main" java.lang.NullPointerException
	at com.xxxx.xxx.xx.x.Gui.swichTab(Gui.java:38)
	at com.xxxx.xxx.xx.x.ToolBarListener.handleEvent(ToolBarListener.java:23)
	at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
	at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
	at com.xxxx.xxx.xx.x.Gui.main(Gui.java:362)

Open in new window



And the lines that are mentioned above are like this:

Line 38 in Gui.java:  

folder.setSelection(1);

Open in new window



Line 23 in ToolBarListener.java:

Gui.swichTab();

Open in new window


Line 362 in Gui.java:

 if (!display.readAndDispatch()) {

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Valeri
Valeri
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar

ASKER

Wow! You are right. I am impressed.

Thanks a lot!