Java by API/org.eclipse.swt.widgets/Table — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:16, 31 мая 2010

new Table(Composite parent, 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();
  }
}





new Table(s, SWT.BORDER | SWT.CHECK | SWT.MULTI | SWT.FULL_SELECTION)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
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;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    final Display d = new Display();
    final Shell s = new Shell(d);
    s.setSize(250, 200);
    GridLayout gl = new GridLayout();
    gl.numColumns = 4;
    s.setLayout(gl);
    final Table t = new Table(s, SWT.BORDER | SWT.CHECK | SWT.MULTI | SWT.FULL_SELECTION);
    final GridData gd = new GridData(GridData.FILL_BOTH);
    gd.horizontalSpan = 4;
    t.setLayoutData(gd);
    t.setHeaderVisible(true);
    final TableColumn tc1 = new TableColumn(t, SWT.LEFT);
    final TableColumn tc2 = new TableColumn(t, SWT.CENTER);
    final 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);
    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" });
    final Text find = new Text(s, SWT.SINGLE | SWT.BORDER);
    final Text replace = new Text(s, SWT.SINGLE | SWT.BORDER);
    final Button replaceBtn = new Button(s, SWT.BORDER | SWT.PUSH);
    replaceBtn.setText("Replace");
    replaceBtn.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        TableItem[] tia = t.getItems();
        for (int i = 0; i < tia.length; i++) {
          if (tia[i].getText(2).equals(find.getText())) {
            tia[i].setText(2, replace.getText());
          }
        }
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Table: addListener(int type, Listener lis)

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
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) {
    Display display = new Display();
    Shell shell = new Shell(display);
  shell.setLayout(new FillLayout());
    
    // Create a table and column
    final Table table = new Table(shell, SWT.VIRTUAL);
    table.setLinesVisible(true);
    table.setHeaderVisible(true);
    TableColumn tc = new TableColumn(table, SWT.NONE);
    tc.setText("Virtual Value");
    tc.setWidth(400);
    // Tell the table how many items it has
    table.setItemCount(100);
    
    // Provide the callback handler--this handler
    // is invoked when the table needs new rows
    table.addListener(SWT.SetData, new Listener() {
      public void handleEvent(Event event) {
        TableItem item = (TableItem) event.item;
        item.setText("data");
      }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Table: getColumnCount()

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) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION);
    for (int i = 0; i < 5; i++) {
      TableColumn column = new TableColumn(table, SWT.NONE);
    }
    for (int i = 0; i < 10; i++) {
      TableItem item = new TableItem(table, SWT.NONE);
      for (int j = 0; j < 5; j++) {
        item.setText(j, "Row " + i + ", Column " + j);
      }
    }
    for (int i = 0, n = table.getColumnCount(); i < n; i++) {
      table.getColumn(i).pack();
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Table: getItem(Point p)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
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 shell = new Shell(d);
    shell.setSize(250, 200);
    shell.setLayout(new FillLayout());
    final Table table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION
        | SWT.HIDE_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    // Create five columns
    for (int i = 0; i < 5; i++) {
      TableColumn column = new TableColumn(table, SWT.CENTER);
      column.setText("Column " + (i + 1));
      column.pack();
    }
    for (int i = 0; i < 5; i++) {
      // Create the row
      new TableItem(table, SWT.NONE);
    }
    table.addMouseListener(new MouseAdapter() {
      public void mouseDown(MouseEvent event) {
        Point pt = new Point(event.x, event.y);
        TableItem item = table.getItem(pt);
        if (item != null) {
          int column = -1;
          for (int i = 0, n = table.getColumnCount(); i < n; i++) {
            Rectangle rect = item.getBounds(i);
            if (rect.contains(pt)) {
              column = i;
              System.out.println(column);
              break;
            }
          }
        }
        
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Table: setHeaderVisible(boolean visible)

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();
  }
}





Table: setLinesVisible(boolean vi)

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Control;
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;
import org.eclipse.swt.widgets.Text;
public class MainClass {
  public static void main(String[] a) {
    final Display d = new Display();
    final Shell shell = new Shell(d);
    shell.setSize(250, 200);
    shell.setLayout(new FillLayout());
    // Number of rows and columns
    final int NUM = 5;
    // Create the table
    final Table table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    for (int i = 0; i < NUM; i++) {
      TableColumn column = new TableColumn(table, SWT.CENTER);
      column.setText("Column " + (i + 1));
      column.pack();
    }
    TableEditor[] colorEditors = new TableEditor[NUM];
    Button[] colorButtons = new Button[NUM];
    for (int i = 0; i < NUM; i++) {
      // Create the row
      final TableItem item = new TableItem(table, SWT.NONE);
      // Create the editor and button
      colorEditors[i] = new TableEditor(table);
      colorButtons[i] = new Button(table, SWT.PUSH);
      // Set attributes of the button
      colorButtons[i].setText("Color...");
      colorButtons[i].ruputeSize(SWT.DEFAULT, table.getItemHeight());
      // Set attributes of the editor
      colorEditors[i].grabHorizontal = true;
      colorEditors[i].minimumHeight = colorButtons[i].getSize().y;
      colorEditors[i].minimumWidth = colorButtons[i].getSize().x;
      colorEditors[i].setEditor(colorButtons[i], item, 0);
      colorButtons[i].addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
          System.out.println("pressed");
        }
      });
    }
    shell.open();
    while (!shell.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}