swt table中添加入力框

來源:互聯網
上載者:User

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, "fei@dhc.com.cn");

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.