SWT里TableEditor的作用是可以在表格里面顯示一些控件,例如列表、按鈕等,有時(shí)候是根據(jù)表格的內(nèi)容在控件上顯示不同內(nèi)容的,如果更新了表格內(nèi)容,就要同時(shí)更新控件,但是表格內(nèi)容可以通過Table控件的removeAll()來清除,而表格中的控件則無法用這個(gè)方法清除,你調(diào)用Table的removeAll()方法,往表格里填入新內(nèi)容后,控件還是上次的控件,但是你一操作那些控件就會(huì)出異常,提示那些控件已經(jīng)disposed。
解決方法是顯式地調(diào)用控件及TableEditor的dispose()方法,在你建立TableEditor的時(shí)候,把它的引用保存起來,把里面的控件的引用也保存起來,到整個(gè)表格需要的清除的時(shí)候,通過引用先把控件dispose掉,再把TableEditor也dispose掉,這樣整個(gè)表格的內(nèi)容就真正清除了。
例如有一個(gè)表格名為table,里面的每一行都有3列,第一列是文本,第二列是Combo,第三列是Button,繪制表格的時(shí)候是這樣的:
其中te和controls都是成員變量,te的類型是TableEditor,controls的類型是ArrayList<Control>。
當(dāng)整個(gè)table要清除內(nèi)容時(shí),可以這樣:
解決方法是顯式地調(diào)用控件及TableEditor的dispose()方法,在你建立TableEditor的時(shí)候,把它的引用保存起來,把里面的控件的引用也保存起來,到整個(gè)表格需要的清除的時(shí)候,通過引用先把控件dispose掉,再把TableEditor也dispose掉,這樣整個(gè)表格的內(nèi)容就真正清除了。
例如有一個(gè)表格名為table,里面的每一行都有3列,第一列是文本,第二列是Combo,第三列是Button,繪制表格的時(shí)候是這樣的:
TableItem ti = new TableItem(table,SWT.NONE);
ti.setText(0,"some string");
te = new TableEditor(table);
Combo combo = new Combo(table,SWT.NONE);
controls.add(combo);
te.setEditor(combo,ti,1);
Button button = new Button(table,SWT.NONE);
controls.add(button);
te.setEditor(button,ti,2);
ti.setText(0,"some string");
te = new TableEditor(table);
Combo combo = new Combo(table,SWT.NONE);
controls.add(combo);
te.setEditor(combo,ti,1);
Button button = new Button(table,SWT.NONE);
controls.add(button);
te.setEditor(button,ti,2);
其中te和controls都是成員變量,te的類型是TableEditor,controls的類型是ArrayList<Control>。
當(dāng)整個(gè)table要清除內(nèi)容時(shí),可以這樣:
//刪除控件
for(Control control:controls){
control.dispose();
}
//刪除TableEditor
te.dispose();
//刪除文本
table.removeAll();
for(Control control:controls){
control.dispose();
}
//刪除TableEditor
te.dispose();
//刪除文本
table.removeAll();