1. 程式人生 > >swt_table 回車可編輯Esc取消

swt_table 回車可編輯Esc取消

ati add tex system open() play 窗口 size ext.get

  1 package 宿舍管理系統;  
  2  
  3 import java.util.Hashtable;  
  4  
  5 import org.eclipse.swt.SWT;  
  6 import org.eclipse.swt.custom.CCombo;  
  7 import org.eclipse.swt.custom.ControlEditor;  
  8 import org.eclipse.swt.custom.TableCursor;  
  9 import org.eclipse.swt.custom.TableEditor;  
 10 import
org.eclipse.swt.custom.ViewForm; 11 import org.eclipse.swt.events.FocusAdapter; 12 import org.eclipse.swt.events.FocusEvent; 13 import org.eclipse.swt.events.KeyAdapter; 14 import org.eclipse.swt.events.KeyEvent; 15 import org.eclipse.swt.events.ModifyEvent; 16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.events.MouseEvent; 18 import org.eclipse.swt.events.MouseListener; 19 import org.eclipse.swt.events.SelectionAdapter; 20 import org.eclipse.swt.events.SelectionEvent; 21 import org.eclipse.swt.layout.FillLayout; 22 import org.eclipse.swt.layout.GridData; 23
import org.eclipse.swt.layout.GridLayout; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Display; 26 import org.eclipse.swt.widgets.Event; 27 import org.eclipse.swt.widgets.Listener; 28 import org.eclipse.swt.widgets.Menu; 29 import org.eclipse.swt.widgets.MenuItem; 30 import org.eclipse.swt.widgets.Shell; 31 import org.eclipse.swt.widgets.Table; 32 import org.eclipse.swt.widgets.TableColumn; 33 import org.eclipse.swt.widgets.TableItem; 34 import org.eclipse.swt.widgets.Text; 35 import org.eclipse.swt.widgets.ToolBar; 36 import org.eclipse.swt.widgets.ToolItem; 37 38 public class jtable { 39 public class TableItemControls{ 40 41 Text text; 42 CCombo combo; 43 TableEditor tableeditor; 44 TableEditor tableeditor1; 45 46 public TableItemControls(Text text, CCombo combo, TableEditor tableeditor, TableEditor tableeditor1) { 47 // super(); 48 this.text = text; 49 this.combo = combo; 50 this.tableeditor = tableeditor; 51 this.tableeditor1 = tableeditor1; 52 } 53 public void dispose() 54 { 55 text.dispose(); 56 combo.dispose(); 57 tableeditor.dispose(); 58 tableeditor1.dispose(); 59 } 60 }; 61 private Shell sShell = null; // @jve:decl-index=0:visual-constraint="10,10" 62 private ViewForm viewForm = null; 63 private ToolBar toolBar = null; 64 private Composite composite = null; 65 private Table table = null; 66 private Menu menu = null; 67 private Hashtable<TableItem, TableItemControls> tablecontrols = new Hashtable<TableItem, TableItemControls>(); 68 69 //創建ViewForm面板放置工具欄和表格 70 private void createViewForm() { 71 viewForm = new ViewForm(sShell, SWT.NONE); 72 viewForm.setTopCenterSeparate(true); 73 createToolBar(); 74 viewForm.setTopLeft(toolBar); 75 createComposite(); 76 viewForm.setContent(composite); 77 } 78 79 //創建工具欄 80 private void createToolBar() { 81 toolBar = new ToolBar(viewForm, SWT.FLAT); 82 final ToolItem add = new ToolItem(toolBar, SWT.PUSH); 83 add.setText("添加"); 84 // add.setImage( ImageFactory.loadImage( toolBar.getDisplay() , ImageFactory.ADD_OBJ)); 85 final ToolItem del = new ToolItem(toolBar, SWT.PUSH); 86 del.setText("刪除"); 87 // del.setImage( ImageFactory.loadImage( toolBar.getDisplay() , ImageFactory.DELETE_OBJ)); 88 final ToolItem back = new ToolItem(toolBar, SWT.PUSH); 89 back.setText("上移"); 90 // back.setImage( ImageFactory.loadImage( toolBar.getDisplay() , ImageFactory.BACKWARD_NAV)); 91 final ToolItem forward = new ToolItem(toolBar, SWT.PUSH); 92 forward.setText("下移"); 93 // forward.setImage( ImageFactory.loadImage( toolBar.getDisplay() , ImageFactory.FORWARD_NAV)); 94 final ToolItem save = new ToolItem(toolBar, SWT.PUSH); 95 save.setText("保存"); 96 // save.setImage( ImageFactory.loadImage( toolBar.getDisplay() , ImageFactory.SAVE_EDIT)); 97 //工具欄按鈕事件處理 98 Listener listener = new Listener(){ 99 public void handleEvent(Event event) { 100 //如果單擊添加按鈕,添加一行,在實際的項目實現中通常是接收輸入的參數,案後添加 101 //這裏為了簡單起見,添加固定的一條記錄 102 if ( event.widget == add ) 103 { 104 TableItem item = new TableItem(table, SWT.NONE); 105 item.setText(new String[] {"鄭六", "女","568","[email protected]"}); 106 } 107 //如果單擊刪除按鈕 108 else if ( event.widget == del) 109 { 110 //首先獲得表格中所有的行 111 TableItem[] items = table.getItems(); 112 //循環所有行 113 for ( int i=items.length-1;i>=0;i-- ){ 114 //如果該行沒有被選中,繼續循環 115 if ( !items[i].getChecked()) 116 continue; 117 //否則選中,查找該表格中是否有該行 118 int index = table.indexOf( items[i]); 119 //如果沒有該行,繼續循環 120 if (index<0) 121 continue; 122 //刪除綁定的控件 123 TableItemControls cons = tablecontrols.get(items[index]); 124 if (cons != null) { 125 cons.dispose(); 126 tablecontrols.remove(items[index]); 127 System.out.println("dispose " + index); 128 } 129 //如果有該行,刪除該行 130 // items[index].dispose(); 131 table.remove( index ); 132 System.out.println("i="+i+", index="+index); 133 System.out.println("行數:" + table.getItemCount()); 134 // table.pack(); 135 } 136 } 137 //如果單擊上移按鈕 138 else if ( event.widget == back) 139 { 140 int selectedRow = table.getSelectionIndex(); 141 if ( selectedRow > 0 ) 142 table.setSelection( selectedRow-1 );//設置選中的行數 143 } 144 //如果單擊下移按鈕 145 else if ( event.widget == forward) 146 { 147 int selectedRow = table.getSelectionIndex(); 148 if ( selectedRow > -1&& selectedRow<table.getItemCount()-1) 149 table.setSelection( selectedRow+1 );//設置選中的行數 150 } 151 //如果單擊保存按鈕 152 else if ( event.widget == save) 153 { 154 TableItem [] items = table.getItems(); 155 //保存到文件或數據庫中,數據持久化,這裏省略 156 for ( int i=0;i<items.length;i++) 157 for (int j=0;j<table.getColumnCount();j++) 158 System.out.println(items[i].getText(j)); 159 } 160 } 161 162 }; 163 //為工具欄的按鈕註冊事件 164 add.addListener( SWT.Selection , listener ); 165 del.addListener( SWT.Selection, listener ); 166 back.addListener( SWT.Selection , listener ); 167 forward.addListener( SWT.Selection, listener ); 168 save.addListener( SWT.Selection, listener ); 169 } 170 //創建放置表格的面板 171 private void createComposite() { 172 GridLayout gridLayout = new GridLayout(); 173 gridLayout.numColumns = 1; 174 composite = new Composite(viewForm, SWT.NONE); 175 composite.setLayout(gridLayout); 176 createTable(); 177 } 178 //創建表格 179 private void createTable() { 180 //表格布局 181 GridData gridData = new org.eclipse.swt.layout.GridData(); 182 gridData.horizontalAlignment = SWT.FILL; 183 gridData.grabExcessHorizontalSpace = true; 184 gridData.grabExcessVerticalSpace = true; 185 gridData.verticalAlignment = SWT.FILL; 186 187 //創建表格,使用SWT.FULL_SELECTION樣式,可同時選中一行 188 table = new Table(composite, SWT.MULTI|SWT.FULL_SELECTION|SWT.CHECK); 189 table.setHeaderVisible(true);//設置顯示表頭 190 table.setLayoutData(gridData);//設置表格布局 191 table.setLinesVisible(true);//設置顯示表格線/* 192 //創建表頭的字符串數組 193 String[] tableHeader = {"姓名","性別","電話","電子郵件"}; 194 for (int i=0;i<tableHeader.length;i++){ 195 TableColumn tableColumn = new TableColumn(table, SWT.NONE); 196 tableColumn.setText( tableHeader[i]); 197 //設置表頭可移動,默認為false 198 tableColumn.setMoveable(true); 199 } 200 //添加三行數據 201 TableItem item = new TableItem(table, SWT.NONE); 202 item.setText(new String[] {"張三", "男","123","[email protected]"}); 203 //設置圖標 204 //item.setImage( ImageFactory.loadImage( table.getDisplay(),ImageFactory.ICON_BOY)); 205 206 for (int i = 0; i < 5; i++) { 207 item = new TableItem(table, SWT.NONE); 208 item.setText(new String[] {"李四", "男","4582","[email protected]"}); 209 } 210 //設置圖標 211 //item.setImage( ImageFactory.loadImage( table.getDisplay(),ImageFactory.ICON_BOY)); 212 213 item = new TableItem(table, SWT.NONE); 214 item.setText(new String[] {"周五", "女","567","[email protected]"}); 215 //設置圖標 216 //item.setImage( ImageFactory.loadImage( table.getDisplay(),ImageFactory.ICON_GIRL)); 217 218 219 220 221 222 223 224 225 226 227 228 //添加可編輯的單元格 229 // /****************************************************** 230 TableItem [] items = table.getItems (); 231 for (int i=0; i<items.length; i++) { 232 233 234 235 236 237 238 //第一列設置,創建TableEditor對象 239 // final TableEditor editor = new TableEditor (table); 240 // //創建一個文本框,用於輸入文字 241 // final Text text = new Text (table, SWT.NONE); 242 // final Text text2 = new Text (table, SWT.NONE); 243 // //將文本框當前值,設置為表格中的值 244 // text.setText(items[i].getText(0)); 245 // 246 // //設置編輯單元格水平填充 247 // editor.grabHorizontal = true; 248 // //關鍵方法,將編輯單元格與文本框綁定到表格的第一列 249 // editor.setEditor(text, items[i], 0); 250 // //當文本框改變值時,註冊文本框改變事件,該事件改變表格中的數據。 251 // //否則即使改變的文本框的值,對表格中的數據也不會影響 252 // text.addModifyListener( new ModifyListener(){ 253 // public void modifyText(ModifyEvent e) { 254 // editor.getItem().setText(1,text.getText()); 255 // } }); 256 // //同理,為第二列綁定下來框CCombo 257 // final TableEditor editor1 = new TableEditor (table); 258 // final CCombo combo = new CCombo (table, SWT.NONE); 259 // combo.add("男"); 260 // combo.add("女"); 261 // combo.setText(items[i].getText(1)); 262 // editor1.grabHorizontal = true; 263 // editor1.setEditor(combo, items[i], 1); 264 // combo.addModifyListener( new ModifyListener(){ 265 // public void modifyText(ModifyEvent e) { 266 // editor1.getItem().setText(1,combo.getText()); 267 // } 268 // }); 269 // // 保存TableItem與綁定Control的對應關系,刪除TableItem時使用 270 // TableItemControls cons = new TableItemControls(text, combo, editor, editor1); 271 // tablecontrols.put(items[i], cons); 272 273 } 274 // *****************************************************/ 275 // /*************************************************** 276 //創建TableCursor對象,使用上下左右鍵可以控制表格 277 final TableCursor cursor = new TableCursor(table, SWT.NONE); 278 //創建可編輯的控件 279 final ControlEditor editor = new ControlEditor(cursor); 280 editor.grabHorizontal = true; 281 editor.grabVertical = true; 282 //為TableCursor對象註冊事件 283 cursor.addSelectionListener( new SelectionAdapter() { 284 //但移動光標,在單元格上單擊回車所觸發的事件 285 public void widgetDefaultSelected(SelectionEvent e) { 286 //創建一個文本框控件 287 final Text text = new Text(cursor, SWT.NONE); 288 //獲得當前光標所在的行TableItem對象 289 TableItem row = cursor.getRow(); 290 //獲得當前光標所在的列數 291 int column = cursor.getColumn(); 292 //當前光標所在單元格的值賦給文本框 293 text.setText(row.getText(column)); 294 //為文本框註冊鍵盤事件 295 text.addKeyListener(new KeyAdapter() { 296 public void keyPressed(KeyEvent e) { 297 //此時在文本框上單擊回車後,這是表格中的數據為修改後文本框中的數據 298 //然後釋放文本框資源 299 if (e.character == SWT.CR) { 300 TableItem row = cursor.getRow(); 301 int column = cursor.getColumn(); 302 row.setText(column, text.getText()); 303 text.dispose(); 304 } 305 //如果在文本框中單擊了ESC鍵,則並不對表格中的數據進行修改 306 if (e.character == SWT.ESC) { 307 text.dispose(); 308 } 309 } 310 }); 311 //註冊焦點事件 312 text.addFocusListener(new FocusAdapter() { 313 //當該文本框失去焦點時,釋放文本框資源 314 public void focusLost(FocusEvent e) { 315 text.dispose(); 316 } 317 }); 318 //將該文本框綁定到可編輯的控件上 319 editor.setEditor(text); 320 //設置文本框的焦點 321 text.setFocus(); 322 } 323 //移動光標到一個單元格上所觸發的事件 324 public void widgetSelected(SelectionEvent e) { 325 table.setSelection(new TableItem[] { cursor.getRow()}); 326 } 327 }); 328 cursor.addMouseListener(new MouseListener() { 329 330 @Override 331 public void mouseDoubleClick(MouseEvent e) { 332 // TODO Auto-generated method stub 333 334 } 335 336 @Override 337 public void mouseDown(MouseEvent e) { 338 if (e.button==3) { // 右鍵按下,顯示右鍵菜單 339 menu.setVisible(true); 340 } 341 } 342 343 @Override 344 public void mouseUp(MouseEvent e) { 345 // TODO Auto-generated method stub 346 347 } 348 349 }); 350 // ******************************************************/ 351 //重新布局表格 352 for (int i=0; i<tableHeader.length; i++) { 353 table.getColumn (i).pack (); 354 } 355 // /**************************************************** 356 //為單元格註冊選中事件 357 table.addSelectionListener( new SelectionAdapter(){ 358 public void widgetSelected(SelectionEvent e) { 359 //獲得所有的行數 360 int total = table.getItemCount(); 361 //循環所有行 362 for ( int i=0;i<total;i++){ 363 TableItem item = table.getItem(i); 364 //如果該行為選中狀態,改變背景色和前景色,否則顏色設置 365 if (table.isSelected( i )){ 366 item.setBackground( sShell.getDisplay().getSystemColor( SWT.COLOR_RED)); 367 item.setForeground( sShell.getDisplay().getSystemColor( SWT.COLOR_WHITE)); 368 }else { 369 item.setBackground( null ); 370 item.setForeground( null ); 371 } 372 } 373 } 374 375 }); 376 // ******************************************************/ 377 } 378 public static void main(String[] args) { 379 //調用主窗口 380 Display display = Display.getDefault(); 381 jtable thisClass = new jtable(); 382 thisClass.createSShell(); 383 thisClass.sShell.open(); 384 while (!thisClass.sShell.isDisposed()) { 385 if (!display.readAndDispatch()) 386 display.sleep(); 387 } 388 // ImageFactory.dispose(); 389 display.dispose(); 390 } 391 392 //創建主窗口 393 private void createSShell() { 394 sShell = new Shell(); 395 sShell.setText("表格窗口"); 396 sShell.setLayout(new FillLayout()); 397 createViewForm(); 398 createMenu(); 399 // sShell.setImage( ImageFactory.loadImage( sShell.getDisplay(), ImageFactory.SAMPLES)); 400 sShell.setSize(new org.eclipse.swt.graphics.Point(500,400)); 401 // sShell.pack(); 402 } 403 //創建上下文菜單 404 private void createMenu() { 405 //創建彈出式菜單 406 menu = new Menu (sShell, SWT.POP_UP); 407 //設置該菜單為表格菜單 408 table.setMenu (menu); 409 //創建刪除菜單項 410 MenuItem del = new MenuItem (menu, SWT.PUSH); 411 del.setText ("刪除"); 412 // del.setImage( ImageFactory.loadImage( sShell.getDisplay(), ImageFactory.DELETE_EDIT)); 413 //為刪除菜單註冊事件,當單擊時,刪除所選擇的行 414 del.addListener (SWT.Selection, new Listener () { 415 public void handleEvent (Event event) { 416 // 此處需添加刪除綁定Control的代碼 417 table.remove (table.getSelectionIndices ()); 418 } 419 }); 420 //創建查看菜單項 421 MenuItem view = new MenuItem (menu, SWT.PUSH); 422 view.setText ("查看"); 423 // view.setImage( ImageFactory.loadImage( sShell.getDisplay(), ImageFactory.SCOPY_EDIT)); 424 //為查看菜單項註冊事件,當單擊時打印出所選的姓名 425 view.addListener (SWT.Selection, new Listener () { 426 public void handleEvent (Event event) { 427 TableItem[] items = table.getSelection(); 428 for (int i=0;i<items.length;i++) 429 System.out.print(items[i].getText()); 430 } 431 }); 432 433 table.setMenu(menu); 434 } 435 436 }

swt_table 回車可編輯Esc取消