swt table中添加入力框

swt table中添加入力框

 

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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 testCheck2 {

 public static void main(String[] args) {
  Display display = new Display();
  final Shell shell = new Shell(display);
  shell.setText("AB");
  shell.setLayout(null);

  final Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
  table.setBounds(0, 0, 268, 106);

  String[] cols = { "A", "B" };
  for (int i = 0; i < cols.length; i++) {
   TableColumn col = new TableColumn(table, SWT.LEFT);
   col.setText(cols[i]);
   col.setWidth(100);
  }
  table.setHeaderVisible(true);
  table.setLinesVisible(true);

  TableItem item1 = new TableItem(table, SWT.NULL);
  item1.setText(0, "Name");
  item1.setText(1, "fei");

  TableItem item2 = new TableItem(table, SWT.NULL);
  item2.setText(0, "E-Mail");
  item2.setText(1, "[email protected]");

  final TableEditor tableEditor = new TableEditor(table);
  tableEditor.grabHorizontal = true;

  table.addSelectionListener(new SelectionAdapter() {

   private static final int EDIT_COLUMN = 1;

   public void widgetSelected(SelectionEvent e) {
    int index = table.getSelectionIndex();
    if (index == -1) {
     return;
    }

    table.setSelection(new int[0]);

    TableItem item = table.getItem(index);

    final Text text = new Text(table, SWT.NONE);
    text.setText(item.getText(EDIT_COLUMN));

    text.addFocusListener(new FocusAdapter() {
     public void focusLost(FocusEvent e) {
      TableItem item = tableEditor.getItem();
      item.setText(EDIT_COLUMN, text.getText());
      text.dispose();
     }
    });
    tableEditor.setEditor(text, item, EDIT_COLUMN);
    text.setFocus();
    text.selectAll();
   }
  });

  shell.setSize(273, 136);
  shell.open();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch()) {
    display.sleep();
   }
  }
  display.dispose();
 }
}

發佈了10 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章