Solved

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

Posted on 2013-06-01
11
474 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

16 Experts available now in Live!

Get 1:1 Help Now