SWT 修改表格單元格內容

Sky_kong 13年前發布 | 5K 次閱讀 P2P mpg123 DBA Stantor-Domodulor JFtp

package com.ui;
import   org.eclipse.swt.*;

public   class   TableTreeSample   {
   
    public   static   void   main   (String   []   args)   {
        Display   display   =   new   Display   ();
        Shell   shell   =   new   Shell(display);
        shell.setText( "TableTree   Sample   1 ");
        shell.setLayout(new   FillLayout());
       

        final   Table   table   =   new   Table(shell,   SWT.BORDER|SWT.FULL_SELECTION);
       
        String[]   cols   =   { "234 ", "234 "};
        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, "0 ");
        item1.setText(1, "點這里修改");
       
        TableItem   item2   =   new   TableItem(table,SWT.NULL);
        item2.setText(0, "1");
        item2.setText(1, "點這里修改");

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

        table.addSelectionListener(new   SelectionAdapter()   {
         // EDIT_COLUMN 是判斷修改的第幾列!!!如果想每列都可以修改。可以循環。
            private   static   final   int   EDIT_COLUMN   =   0;
           
            public   void   widgetSelected(SelectionEvent   e){
             // EDIT_COLUMN 是判斷鼠標選中第幾行!!!
                int   index   =   table.getSelectionIndex();
                System.out.println("這個事是第幾行~~"+index);
                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(250,100);
        shell.open();
        while   (!shell.isDisposed   ()){
            if   (!display.readAndDispatch   ()){
                display.sleep   ();
            }
        }
        display.dispose   ();
    }
}

 本文由用戶 Sky_kong 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!