Home Tutorials Swt Create Table in SWT
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Create Table in SWT

Advertisement
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 SimpleTableExample.java

import java.util.*;
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
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");
  createContents(shell);
  shell.pack();
  shell.open();
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch()) {
  display.sleep();
  }
  }
  display.dispose();
  }
  private void createContents(Composite composite) {
  composite.setLayout(new FillLayout());
  final Table table = new Table(composite, SWT.NONE);
  table.setHeaderVisible(true);
  table.setLinesVisible(true);
  TableColumn[] column = new TableColumn[3];
  column[0] = new TableColumn(table, SWT.NONE);
  column[0].setText("Name");
  
  column[1] = new TableColumn(table, SWT.NONE);
  column[1].setText("Country");
  
  column[2] = new TableColumn(table, SWT.RIGHT);
  column[2].setText("Ranking");
  
  fillTable(table);
  for (int i = 0, n = column.length; i < n; i++) {
  column[i].pack();
  }
  }
  private void fillTable(Table table) {
  table.setRedraw(false);
 
  for(Iterator iterator = players.iterator();iterator.hasNext();){
  Player player = (Player) iterator.next();
  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()));
  }
  table.setRedraw(true);
  }
  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) {
  this.name = name;
  this.country = 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

     

Advertisement

Liked it!  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.

Posted on: June 8, 2009

Ask Questions?    Discuss: Create Table in SWT   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com