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","zhengliu@sina.com"}); 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","zhangsan@sina.com"}); 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","lisi@sina.com"}); 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","zhouwu@sina.com"}); 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 }