Create Table in SWT

In this section, you will study how to create a table.

Create Table in SWT


In this section, you will study how to create a table.

SWT provides the classes Table, TableItem, TableColumn of package org.eclipse.swt.widgets to create a table. In the given example, we have create an ArrayList of Tennis players along with their countries and rankings. 

The method composite.setLayout() sets the layout of table. The method table.setHeaderVisible(true) makes the header of the table visible. The method table.setLinesVisible(true) makes the line visible between the rows and columns. The class TableColumn creates the columns and the method column.setText() sets the header name. The method table.setRedraw(false) causes drawing operations to be ignored. If it is set to true, it performs drawing operations. The Iterator iterates the Array List. The TableItem class sets the items to the table. The method String.valueOf(player.getRanking()) converts the integer value to String.



Here is the code of

import java.util.*;
import org.eclipse.swt.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class SimpleTableExample {

  private java.util.List players;

  public SimpleTableExample() {
  players = new ArrayList();
  players.add(new Player("Roger Federer", "Switzerland", 1));
  players.add(new Player("Rafael Nadal", "Spain", 2));
  players.add(new Player("Andy Roddick", "USA", 3));
  players.add(new Player("Lleyton Hewitt", "Australia", 4));
  players.add(new Player("Nikolay Davydenko", "Russia", 5));
  players.add(new Player("David Nalbandian", "ARG", 6));
  players.add(new Player("Andre Agassi", "USA", 7));
  players.add(new Player("Guillermo Coria", "ARG", 8));

  public void run() {
  Display display = new Display();
  Shell shell = new Shell(display);
  shell.setText("Tennis players");
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch()) {
  private void createContents(Composite composite) {
  composite.setLayout(new FillLayout());
  final Table table = new Table(composite, SWT.NONE);
  TableColumn[] column = new TableColumn[3];
  column[0] = new TableColumn(table, SWT.NONE);
  column[1] = new TableColumn(table, SWT.NONE);
  column[2] = new TableColumn(table, SWT.RIGHT);
  for (int i = 0, n = column.length; i < n; i++) {
  private void fillTable(Table table) {
  for(Iterator iterator = players.iterator();iterator.hasNext();){
  Player player = (Player);
  TableItem item = new TableItem(table, SWT.NONE);
  int c = 0;
  item.setText(c++, player.getName());
  item.setText(c++, player.getCountry());
  item.setText(c++, String.valueOf(player.getRanking()));
  public static void main(String[] args) {
  new SimpleTableExample().run();
 class Player {
  private String name;
  private String country;
  private int ranking;

  public Player(String name, String country, int ranking) { = name; = country;
  this.ranking = ranking;
  public int getRanking() {
  return ranking;
  public String getName() {
  return name;
  public String getCountry() {
  return country;

Output will be displayed as:

Download Source Code


Share on Google+Share on Google+

Create Table in SWT

Posted on: June 8, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Create Table in SWT   View All Comments

Post your Comment

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