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

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

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

new TableCursor(Table parent, int style)

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
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;
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());
    final Table table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    for (int i = 0; i < 3; i++) {
      TableColumn column = new TableColumn(table, SWT.CENTER);
      column.setText("Press Enter to Edit " + (i + 1));
      column.pack();
    }
    for (int i = 0; i < 3; i++) {
      new TableItem(table, SWT.NONE);
    }
    final TableCursor cursor = new TableCursor(table, SWT.NONE);
    final ControlEditor editor = new ControlEditor(cursor);
    editor.grabHorizontal = true;
    editor.grabVertical = true;
    cursor.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        table.setSelection(new TableItem[] { cursor.getRow()});
      }
      public void widgetDefaultSelected(SelectionEvent event) {
        final Text text = new Text(cursor, SWT.NONE);
        text.setFocus();
        text.setText(cursor.getRow().getText(cursor.getColumn()));
        text.setFocus();
        text.addKeyListener(new KeyAdapter() {
          public void keyPressed(KeyEvent event) {
            switch (event.keyCode) {
            case SWT.CR:
              cursor.getRow().setText(cursor.getColumn(), text.getText());
            }
          }
        });
        editor.setEditor(text);
      }
    });
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





TableCursor: addSelectionListener(SelectionListener lis)

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
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;
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());
    final Table table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    for (int i = 0; i < 3; i++) {
      TableColumn column = new TableColumn(table, SWT.CENTER);
      column.setText("Press Enter to Edit " + (i + 1));
      column.pack();
    }
    for (int i = 0; i < 3; i++) {
      new TableItem(table, SWT.NONE);
    }
    final TableCursor cursor = new TableCursor(table, SWT.NONE);
    final ControlEditor editor = new ControlEditor(cursor);
    editor.grabHorizontal = true;
    editor.grabVertical = true;
    cursor.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        table.setSelection(new TableItem[] { cursor.getRow()});
      }
      public void widgetDefaultSelected(SelectionEvent event) {
        final Text text = new Text(cursor, SWT.NONE);
        text.setFocus();
        text.setText(cursor.getRow().getText(cursor.getColumn()));
        text.setFocus();
        text.addKeyListener(new KeyAdapter() {
          public void keyPressed(KeyEvent event) {
            switch (event.keyCode) {
            case SWT.CR:
              cursor.getRow().setText(cursor.getColumn(), text.getText());
            }
          }
        });
        editor.setEditor(text);
      }
    });
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}