Java by API/org.eclipse.swt.widgets/TableItem

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

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





TableItem: setBackground(Color col)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
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());
            tia[i].setBackground(new Color(d,127,178,127));
          }
        }
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





TableItem: setText(String[] textArray)

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





TreeItem: setExpanded(boolean expanded)

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
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 Tree tree = new Tree(shell, SWT.SINGLE);
    for (int i = 0; i < 3; i++) {
      TreeItem iItem = new TreeItem(tree, SWT.NONE);
      iItem.setText("Edit me by pressing F2 " + (i + 1));
      iItem.setExpanded(true);
    }
    final TreeEditor editor = new TreeEditor(tree);
    editor.horizontalAlignment = SWT.LEFT;
    editor.grabHorizontal = true;
    tree.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent event) {
        if (event.keyCode == SWT.F2 && tree.getSelectionCount() == 1) {
          final TreeItem item = tree.getSelection()[0];
          final Text text = new Text(tree, SWT.NONE);
          text.setText(item.getText());
          text.setFocus();
          text.addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent event) {
              item.setText(text.getText());
              text.dispose();
            }
          });
          editor.setEditor(text, item);
        }
      }
    });
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}