1. 程式人生 > >SWT雙擊table單元格可編輯、變為下拉框

SWT雙擊table單元格可編輯、變為下拉框

最近一年都是在做plugin開發,碰到的問題也很多,之前碰到個有關table可編輯的,似乎答案甚少,幾乎找不到,這裡就將自己的經驗分享一下。

首先是雙擊表格,使之變為可編輯框,修改之後恢復預設設定


程式碼如下:

table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				
				if(table.getSelectionCount() != 0){
					final TableItem item = table.getItem(table.getSelectionIndex());
					//獲取滑鼠點選的位置
					Point point = new Point(e.x, e.y);
					//滑鼠點選所在cell的列
					int tmpCol = 0;
					//獲取所點選的cell
					for(int i = 0; i < table.getColumnCount(); i++){
						Rectangle rect = item.getBounds(i);
						if (rect.contains(point)) {
							tmpCol = i;
							break;
						}
					}
					final int selectCol = tmpCol;
					final TableEditor editor = new TableEditor(table); 
					Control oldEditor = editor.getEditor();
					if (oldEditor != null){
						oldEditor.dispose();
					}
					final Text changeItemText = new Text(table, SWT.NONE);
					changeItemText.computeSize(SWT.DEFAULT, table.getItemHeight());
					editor.grabHorizontal = true;
					editor.minimumHeight = changeItemText.getSize().y;
					editor.minimumWidth = changeItemText.getSize().x;
					editor.setEditor(changeItemText, item, selectCol);
					changeItemText.setText(item.getText(selectCol));
					changeItemText.forceFocus();
					
					//編輯後變回表格樣式
					changeItemText.addFocusListener(new FocusAdapter() {
						@Override
						public void focusLost(FocusEvent focusevent) {
							Control defaultEditor = editor.getEditor();
							editor.setEditor(defaultEditor, item, selectCol);
							item.setText(selectCol, changeItemText.getText());
							changeItemText.dispose();
						}
					});
				}
			}
		});

有了這個基礎,要將其變為下拉框就無限簡單了,只需要將Text替換為Combo就完事了,下面是修改後的圖片。


修改後的程式碼如下

table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				
				if(table.getSelectionCount() != 0){
					final TableItem item = table.getItem(table.getSelectionIndex());
					//獲取滑鼠點選的位置
					Point point = new Point(e.x, e.y);
					//滑鼠點選所在cell的列
					int tmpCol = 0;
					//獲取所點選的cell
					for(int i = 0; i < table.getColumnCount(); i++){
						Rectangle rect = item.getBounds(i);
						if (rect.contains(point)) {
							tmpCol = i;
							break;
						}
					}
					final int selectCol = tmpCol;
					final TableEditor editor = new TableEditor(table); 
					Control oldEditor = editor.getEditor();
					if (oldEditor != null){
						oldEditor.dispose();
					}
					//這裡只需講Text換成Combo就行
					//final Text changeItemText = new Text(table, SWT.NONE);
					final Combo comOptionValue = new Combo(table, SWT.NONE);
					//對Combo進行賦值
					comOptionValue.setItems(new String[]{"選項1","選項2","選項3","選項4"});
					comOptionValue.computeSize(SWT.DEFAULT, table.getItemHeight());
					editor.grabHorizontal = true;
					editor.minimumHeight = comOptionValue.getSize().y;
					editor.minimumWidth = comOptionValue.getSize().x;
					editor.setEditor(comOptionValue, item, selectCol);
					comOptionValue.setText(item.getText(selectCol));
					comOptionValue.forceFocus();
					
					//編輯後變回表格樣式
					comOptionValue.addFocusListener(new FocusAdapter() {
						@Override
						public void focusLost(FocusEvent focusevent) {
							Control defaultEditor = editor.getEditor();
							editor.setEditor(defaultEditor, item, selectCol);
							item.setText(selectCol, comOptionValue.getText());
							comOptionValue.dispose();
						}
					});
				}
			}
		});