TableColumn: addSelectionListener(SelectionListener lis) : TableColumn « org.eclipse.swt.widgets « Java by API






TableColumn: addSelectionListener(SelectionListener lis)

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class MainClass {
  private StudentComparator comparator = new StudentComparator();

  private java.util.List students = new ArrayList();

  public MainClass() {
    comparator.setColumn(StudentComparator.FIRST_NAME);
    comparator.setDirection(StudentComparator.ASCENDING);
    
    students.add(new Student("A", "H", 0.73f));
    students.add(new Student("B", "G", 0.65f));
    students.add(new Student("C", "F", 0.11f));
    students.add(new Student("D", "E", 0.69f));
    students.add(new Student("E", "D", 0.76f));
    students.add(new Student("F", "C", 0.99f));
    students.add(new Student("G", "B", 0.55f));
    students.add(new Student("H", "A", 0.95f));
  }

  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Player Table");

    shell.setLayout(new FillLayout());

    final Table table = new Table(shell, SWT.NONE);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    TableColumn[] columns = new TableColumn[3];
    columns[0] = new TableColumn(table, SWT.NONE);
    columns[0].setText("First Name");
    columns[0].addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        comparator.setColumn(StudentComparator.FIRST_NAME);
        comparator.reverseDirection();
        fillTable(table);
      }
    });

    columns[1] = new TableColumn(table, SWT.NONE);
    columns[1].setText("Last Name");
    columns[1].addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        comparator.setColumn(StudentComparator.LAST_NAME);
        comparator.reverseDirection();
        fillTable(table);
      }
    });

    columns[2] = new TableColumn(table, SWT.RIGHT);
    columns[2].setText("Average");
    columns[2].addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        comparator.setColumn(StudentComparator.AVERAGE);
        comparator.reverseDirection();
        fillTable(table);
      }
    });

    fillTable(table);

    for (int i = 0, n = columns.length; i < n; i++) {
      columns[i].pack();
    }
    
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }

  private void fillTable(Table table) {
    // Turn off drawing to avoid flicker
    table.setRedraw(false);

    // We remove all the table entries, sort our
    // rows, then add the entries
    table.removeAll();
    Collections.sort(students, comparator);
    for (Iterator itr = students.iterator(); itr.hasNext();) {
      Student student = (Student) itr.next();
      TableItem item = new TableItem(table, SWT.NONE);
      int c = 0;
      item.setText(c++, student.getFirstName());
      item.setText(c++, student.getLastName());
      item.setText(c++, String.valueOf(student.getAverage()));
    }

    // Turn drawing back on
    table.setRedraw(true);
  }
  public static void main(String[] args) {
    new MainClass().run();
  }
}

class StudentComparator implements Comparator {
  /** Constant for First Name column */
  public static final int FIRST_NAME = 0;

  /** Constant for Last Name column */
  public static final int LAST_NAME = 1;

  /** Constant for Batting Average column */
  public static final int AVERAGE = 2;

  /** Constant for ascending */
  public static final int ASCENDING = 0;

  /** Constant for descending */
  public static final int DESCENDING = 1;

  private int column;

  private int direction;

  public int compare(Object obj1, Object obj2) {
    int rc = 0;
    Student p1 = (Student) obj1;
    Student p2 = (Student) obj2;

    switch (column) {
    case FIRST_NAME:
      rc = p1.getFirstName().compareTo(p2.getFirstName());
      break;
    case LAST_NAME:
      rc = p1.getLastName().compareTo(p2.getLastName());
      break;
    case AVERAGE:
      rc = (p1.getAverage() < p2.getAverage()) ? -1 : 1;
      break;
    }

    if (direction == DESCENDING) {
      rc = -rc;
    }
    return rc;
  }

  public void setColumn(int column) {
    this.column = column;
  }

  public void setDirection(int direction) {
    this.direction = direction;
  }

  public void reverseDirection() {
    direction = 1 - direction;
  }
}

class Student {
  private String firstName;

  private String lastName;

  private float average;

  public Student(String firstName, String lastName, float a) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.average = a;
  }

  public float getAverage() {
    return average;
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }
}


           
       








Related examples in the same category

1.new TableColumn(Table table, int style)
2.TableColumn: setWidth(int width)
3.TableColumn: setText(String s)