Home Tutorials Swt Create Tabs in Java using SWT



Create Tabs in Java using SWT
Posted on: October 4, 2008 at 12:00 AM
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

     

Related Tags for Create Tabs in Java using SWT:
javacdiffiostructswttabspagevalueusingthiscreatetabifexamplewithvaluestoexameacheilitindifferentmnttrjafteresthroughagemehowhrsamexaxampsatisllivmplgofolloweastrsawtvassamssamrenteateachthswavstabablaluaffepleplonolo


More Tutorials from this section

Ask Questions?    Discuss: Create Tabs in Java using SWT  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.