Share on Google+Share on Google+

Create Tabs in Java using SWT

Advertisement
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

     

Advertisements

Advertisement

Posted on: October 4, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Create Tabs in Java using SWT  

Post your Comment


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