Solved

How to create a window with 3 tabs in JAVA SWT?

Posted on 2013-06-01
11
483 Views
Last Modified: 2013-06-04
Hi,
I just started to work with JAVA SWT package.

Can you please send me a sample code which creates a window that has 3 tabs in it by using swt package?

And the tab names will be A, B and C.



Thanks,
0
Comment
Question by:Tolgar
  • 5
  • 5
11 Comments
 
LVL 11

Expert Comment

by:wrmichael
ID: 39214312
package de.vogella.swt.widgets;

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.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class CTabFolderExample {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);

        shell.setLayout(new GridLayout());
        // SWT.BOTTOM to show at the bottom
        CTabFolder folder = new CTabFolder(shell, SWT.BOTTOM);
        GridData data = new GridData(GridData.FILL, 
                GridData.FILL, true, true,
                2, 1);
        folder.setLayoutData(data);
        CTabItem cTabItem1 = new CTabItem(folder, SWT.NONE);
        cTabItem1.setText("Tab1");
        CTabItem cTabItem2 = new CTabItem(folder, SWT.NONE);
        cTabItem2.setText("Tab2");
        CTabItem cTabItem3 = new CTabItem(folder, SWT.NONE);
        cTabItem3.setText("Tab3");

        Text text = new Text(folder, SWT.BORDER);
        text.setText("Hello");
        cTabItem1.setControl(text);

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}
 

Open in new window

0
 
LVL 11

Expert Comment

by:wrmichael
ID: 39214313
0
 

Author Comment

by:Tolgar
ID: 39216540
How can I move these tab to the top left corner of the page?

 and is there a way to set the size of the initial window to a certain value?
0
Technology Partners: 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!

 

Author Comment

by:Tolgar
ID: 39216569
Actually, the thing that I try to create looks like the one in the attachment.

Thanks,
A.png
0
 
LVL 11

Expert Comment

by:wrmichael
ID: 39216571
without digging into the API it looks like you could replace SWT.BOTTOM with SWT.TOP

API reference can be found here:

http://www.eclipse.org/swt/docs.php

if you can't locate it I can do search through it.
0
 

Author Comment

by:Tolgar
ID: 39216575
Ok. I made the tabs on the top. Now, how can I create the icons in the first tab as shown in the mock up that I sent?
0
 
LVL 11

Expert Comment

by:wrmichael
ID: 39216590
this should help you understand how that works.

http://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html

Do you have a good IDE that you are able to test in?  

it helps to tinker and see what the different options can do and how they work.
0
 

Author Comment

by:Tolgar
ID: 39217210
Ok. Now I managed to add the button but I cannot generate the tabs anymore.

What am I missing?

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.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;

public class Gui {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);

        // 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.
        new Button(shell, SWT.PUSH).setText("B1");
        new Button(shell, SWT.PUSH).setText("Wide Button 2");
        new Button(shell, SWT.PUSH).setText("Button 3");
        shell.pack();
        shell.open();
        
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        
        FillLayout fillLayout = new FillLayout();
        fillLayout.type = SWT.VERTICAL;
        shell.setLayout(fillLayout);
        new Button(shell, SWT.PUSH).setText("B1");
        new Button(shell, SWT.PUSH).setText("Wide Button 2");
        new Button(shell, SWT.PUSH).setText("Button 3");
        
        fillLayout.type = SWT.HORIZONTAL;
                
        RowLayout rowLayout = new RowLayout();
        rowLayout.wrap = false;
        rowLayout.pack = false;
        rowLayout.justify = true;
        rowLayout.type = SWT.VERTICAL;
        rowLayout.marginLeft = 5;
        rowLayout.marginTop = 5;
        rowLayout.marginRight = 5;
        rowLayout.marginBottom = 5;
        rowLayout.spacing = 0;
        shell.setLayout(rowLayout);     
        
        rowLayout.wrap = true;
        rowLayout.pack = true;
        rowLayout.justify = false;
        rowLayout.type = SWT.HORIZONTAL;
        
        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("Tab1");
        CTabItem cTabItem2 = new CTabItem(folder, SWT.NONE);
        cTabItem2.setText("Tab2");
        CTabItem cTabItem3 = new CTabItem(folder, SWT.NONE);
        cTabItem3.setText("Tab3");

        Text text = new Text(folder, SWT.BORDER);
        text.setText("You can type the list of files in this field");
        cTabItem1.setControl(text);

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}

Open in new window

0
 
LVL 11

Expert Comment

by:wrmichael
ID: 39217846
I'll have to leave that for another expert.   I don't have an swt environment setup.
0
 
LVL 16

Accepted Solution

by:
Valeri earned 500 total points
ID: 39218631
here is your code reworked in order to meet your needs. But I think that you have to read some very short tutorial about swt. for example this: http://www.vogella.com/articles/SWT/article.html

A lot of good examples here:
http://www.java2s.com/Tutorial/Java/0280__SWT/Catalog0280__SWT.htm

You have to be familiar with:
1. what is Display and Shell
2. How components are organised in hierarchy, in your case CTabFolder is parent of all other components, the three tabs. each tab has assigned it's own "Composite" and and the respective tab is parent of the composite. The same is with Buttons, each button is a child of the respective composite etc... So get familiar with parent -> child hierarchy. The above tutorial will take you no more that 2-3 hours. Here is your code:

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;

public class Gui {
    public static void main(String[] args) {
        Display display = new Display();
        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,
                20, 10);
        folder.setLayoutData(data);
        CTabItem cTabItem1 = new CTabItem(folder, SWT.NONE);
        cTabItem1.setText("Tab1");
        CTabItem cTabItem2 = new CTabItem(folder, SWT.NONE);
        cTabItem2.setText("Tab2");
        CTabItem cTabItem3 = new CTabItem(folder, SWT.NONE);
        cTabItem3.setText("Tab3");
               
        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.
        new Button(compTab1, SWT.PUSH).setText("B1");
        new Button(compTab1, SWT.PUSH).setText("Wide Button 1");
        new Button(compTab1, SWT.PUSH).setText("Button 1");
       
        compTab1.setLayout(layout);
        cTabItem1.setControl(compTab1);
       
        Composite compTab2 = new Composite(folder, SWT.NULL);
       
        FillLayout fillLayout = new FillLayout();
        fillLayout.type = SWT.VERTICAL;
        new Button(compTab2, SWT.PUSH).setText("B2");
        new Button(compTab2, SWT.PUSH).setText("Wide Button 2");
        new Button(compTab2, SWT.PUSH).setText("Button 2");
       
        compTab2.setLayout(fillLayout);
        cTabItem2.setControl(compTab2);
       
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}
0
 

Author Comment

by:Tolgar
ID: 39219051
Thanks!
0

Featured Post

Independent Software Vendors: 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
Custom Java Application connects to database: Which one of the 20? 4 41
ejb wildfly example 2 69
hashmap order 17 40
jmeter usage 4 31
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

679 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