Posted on 2007-07-20 21:21
kooyee 阅读(7516)
评论(4) 编辑 收藏 所属分类:
GUI骨衣
对于select event在SWT table中
TableItem item = (TableItem)e.item;
这个是建立一个 item的指向,而不是一个实质的item。 所以当清空table中的items后,再调用这个item会返回" widget is disposed" Excepion异常。比如调用
item.addDisposeListener(new org.eclipse.swt.events.DisposeListener()...{
public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) ...{
...
}
});
Add action listener to Table Button Cell Editor 添加button
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class TableEditorButtonActionListener {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Text Table Editor");
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.SINGLE | SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
for (int i = 0; i < 5; i++) {
TableColumn column = new TableColumn(table, SWT.CENTER);
column.setText("Column " + (i + 1));
column.pack();
}
// Create five table editors for color
TableEditor[] colorEditors = new TableEditor[5];
// Create five buttons for changing color
Button[] colorButtons = new Button[5];
for (int i = 0; i < 5; i++) {
final TableItem item = new TableItem(table, SWT.NONE);
colorEditors[i] = new TableEditor(table);
colorButtons[i] = new Button(table, SWT.PUSH);
colorButtons[i].setText("Color");
colorButtons[i].computeSize(SWT.DEFAULT, table.getItemHeight());
colorEditors[i].grabHorizontal = true;
colorEditors[i].minimumHeight = colorButtons[i].getSize().y;
colorEditors[i].minimumWidth = colorButtons[i].getSize().x;
colorEditors[i].setEditor(colorButtons[i], item, 0);
colorButtons[i].addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
ColorDialog dialog = new ColorDialog(shell);
RGB rgb = dialog.open();
if (rgb != null) {
table.setBackground(new Color(shell.getDisplay(), rgb));
}
}
});
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
如果不加这行 colorEditors[i].minimumHeight
= colorButtons[i].getSize().y;
则editor不会按照control的高度设置,而是按照table单元格高度填入button等控件。所以想要控件的大小符合单元格大小(控件大小不会溢出到单元格外),不需要添加这一行。如果加入Combo,则要使用CCombo才能改变高度,因为在win32系统中Combo的高度被禁止改变同理添加combo, checkbox
TableItem item = new TableItem(table,0);
TableEditor editor = new TableEditor (table);
//添加combo
final CCombo tax = new CCombo(table, SWT.NONE);
for(int i=0;i<10;i++)
tax.add("T"+i);
tax.setText(rs.getString(2));
tax.pack();
editor.grabVertical=false;
editor.minimumWidth = tax.getSize ().x;
editor.minimumHeight = tax.getSize().y;
editor.horizontalAlignment = SWT.TOP;
editor.setEditor(tax, item, 1);
//添加check box
editor = new TableEditor (spUi.tablePrice);
final Button check = new Button(spUi.tablePrice, SWT.CHECK);
check.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
if(Integer.parseInt(rs.getString(5))==1)
check.setSelection(true);
else
check.setSelection(false);
check.pack();
editor.minimumWidth = check.getSize ().x;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor(check, item, 4);
item.addDisposeListener(new org.eclipse.swt.events.DisposeListener(){
public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) {
//editor.dispose();当table刷新后,旧的控件清除掉。否则item remove后控件还会在原位
tax.dispose();
check.dispose();
}
});