Java by API/org.eclipse.swt.custom/TableEditor

Материал из Java эксперт
Версия от 14:43, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

new TableEditor(Table table)

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





TableEditor: setEditor(Control control, TableItem item, int index)

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