Java by API/org.eclipse.swt.widgets/TableColumn
Содержание
new TableColumn(Table table, int style)
import org.eclipse.swt.SWT;
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 {
public static void main(String[] a) {
final Display d = new Display();
final Shell s = new Shell(d);
s.setSize(250, 200);
s.setText("A Table Shell Example");
s.setLayout(new FillLayout());
Table t = new Table(s, SWT.BORDER);
TableColumn tc1 = new TableColumn(t, SWT.CENTER);
TableColumn tc2 = new TableColumn(t, SWT.CENTER);
TableColumn tc3 = new TableColumn(t, SWT.CENTER);
tc1.setText("First Name");
tc2.setText("Last Name");
tc3.setText("Address");
tc1.setWidth(70);
tc2.setWidth(70);
tc3.setWidth(80);
t.setHeaderVisible(true);
TableItem item1 = new TableItem(t, SWT.NONE);
item1.setText(new String[] { "A", "B", "Address 1" });
TableItem item2 = new TableItem(t, SWT.NONE);
item2.setText(new String[] { "C", "D", "Address 2" });
TableItem item3 = new TableItem(t, SWT.NONE);
item3.setText(new String[] { "E", "F", "Address 3" });
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
}
TableColumn: addSelectionListener(SelectionListener lis)
import java.util.ArrayList;
import java.util.Collections;
import java.util.ruparator;
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().rupareTo(p2.getFirstName());
break;
case LAST_NAME:
rc = p1.getLastName().rupareTo(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;
}
}
TableColumn: setText(String s)
import org.eclipse.swt.SWT;
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 {
public static void main(String[] a) {
final Display d = new Display();
final Shell s = new Shell(d);
s.setSize(250, 200);
s.setText("A Table Shell Example");
s.setLayout(new FillLayout());
Table t = new Table(s, SWT.BORDER);
TableColumn tc1 = new TableColumn(t, SWT.CENTER);
TableColumn tc2 = new TableColumn(t, SWT.CENTER);
TableColumn tc3 = new TableColumn(t, SWT.CENTER);
tc1.setText("First Name");
tc2.setText("Last Name");
tc3.setText("Address");
tc1.setWidth(70);
tc2.setWidth(70);
tc3.setWidth(80);
t.setHeaderVisible(true);
TableItem item1 = new TableItem(t, SWT.NONE);
item1.setText(new String[] { "A", "B", "Address 1" });
TableItem item2 = new TableItem(t, SWT.NONE);
item2.setText(new String[] { "C", "D", "Address 2" });
TableItem item3 = new TableItem(t, SWT.NONE);
item3.setText(new String[] { "E", "F", "Address 3" });
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
}
TableColumn: setWidth(int width)
import org.eclipse.swt.SWT;
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 {
public static void main(String[] a) {
final Display d = new Display();
final Shell s = new Shell(d);
s.setSize(250, 200);
s.setText("A Table Shell Example");
s.setLayout(new FillLayout());
Table t = new Table(s, SWT.BORDER);
TableColumn tc1 = new TableColumn(t, SWT.CENTER);
TableColumn tc2 = new TableColumn(t, SWT.CENTER);
TableColumn tc3 = new TableColumn(t, SWT.CENTER);
tc1.setText("First Name");
tc2.setText("Last Name");
tc3.setText("Address");
tc1.setWidth(70);
tc2.setWidth(70);
tc3.setWidth(80);
t.setHeaderVisible(true);
TableItem item1 = new TableItem(t, SWT.NONE);
item1.setText(new String[] { "A", "B", "Address 1" });
TableItem item2 = new TableItem(t, SWT.NONE);
item2.setText(new String[] { "C", "D", "Address 2" });
TableItem item3 = new TableItem(t, SWT.NONE);
item3.setText(new String[] { "E", "F", "Address 3" });
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
}