More Tutorials| Bioinformatics| Open Source| Photoshop| Questions? | Software Development
 

Create Tabs in Java using SWT

This Tab Example in Java, will teach you how to create tabs using SWT in Java. After going through the example you will be able to create different tabs with different values on the same page. Just follow the given instruction.

Create Tabs in Java using SWT

                         

This Tab Example in Java, will teach you how to create tabs using SWT in Java. After going through the example you will be able to create different tabs with different values on the same page. Just follow the given instruction.

SWT provides the classes TabFolder and TabItem to create tabs. The class TabFolder allows the user to select a page from set of pages which is in the form of tabs. The class TabItem adds a page in the folder in the form of tab. the method item.setText() sets the text to the tabItem. 

The method tabFolder.setSize() sets the size of tabFolder. The method getSelection() returns an array of TabItem that are currently selected.

Following code create the tabs:

for (int i=1; i<5; i++) {
TabItem item = new TabItem (tabFolder, SWT.NULL);
item.setText ("Tab" + i);

Here is the code of TabExample.java

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.FillLayout;

public class TabExample {
  Display display = new Display();
  Shell shell = new Shell(display);
  
  public TabExample() {
    shell.setLayout(new FillLayout());
  shell.setText("Tabs");
   
    final TabFolder tabFolder = new TabFolder (shell, SWT.NONE);
    for (int i=1; i<5; i++) {
     TabItem item = new TabItem (tabFolder, SWT.NULL);
     item.setText ("Tab" + i);
     Text text = new Text(tabFolder, SWT.BORDER | SWT.MULTI);
     text.setText("This is Tab "+i);
     item.setControl(text);
 }
     tabFolder.setSize (250150);
     tabFolder.addSelectionListener(new SelectionListener() {
     public void widgetSelected(SelectionEvent e) {
     System.out.println("You have selected:"+ tabFolder.getSelection()
          [
0].toString());
     }
     public void widgetDefaultSelected(SelectionEvent e) {
        widgetSelected(e);
      }
    });
  shell.setSize(200150);
    shell.open();
    
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
      display.sleep();
      }
    }
    display.dispose();
  }
   public static void main(String[] args) {
    new TabExample();
  }
}

Output will be displayed as:

Download Source Code

                         

» View all related tutorials
Related Tags: c eclipse ide class button io swt get ip widgets order vi widget using cli border this id package rad

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.