Solved

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

Posted on 2013-06-11
5
381 Views
Last Modified: 2013-06-13
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

0
Comment
Question by:Tolgar
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:Valeri
ID: 39241319
see again the files attached to this question:
http://www.experts-exchange.com/Programming/Languages/Java/Q_28154257.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".
0
 

Author Comment

by:Tolgar
ID: 39242374
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?
0
 

Author Comment

by:Tolgar
ID: 39242484
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

0
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 39244795
looks like "folder" is null...?! probably you have another code, becasue I moved "folder" as a member of the class, till now it was a local variable in the main method.
most probably it's still local variable in your code?! I don't know?! may be you have such as line:
CTabFolder folder = new CTabFolder(shell, SWT.TOP);
now it has to be just:
folder = new CTabFolder(shell, SWT.TOP);
0
 

Author Comment

by:Tolgar
ID: 39244843
Wow! You are right. I am impressed.

Thanks a lot!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

706 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

15 Experts available now in Live!

Get 1:1 Help Now