`
hustlong
  • 浏览: 121445 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

SWT的Table对单元格添加可编辑属性

阅读更多
首先是初始化Display , shell ,然后建立table,初始化table:
主要代码贴在下面了:
display = new Display();
font1=new Font(display,"宋体",10,SWT.BOLD);
font2=new Font(display,"宋体",12, SWT.ITALIC);
font3=new Font(display,"宋体",12,SWT.BOLD );
shell = new Shell(display);
shell.setBounds(0, 0, 950, 600);
shell.setText("电信系学生信息管理系统(Designed By Ikeel)");
shell.setImage(new Image(null, "image/book.jpg"));
shell.setBackgroundImage(new Image(null, "image/13.jpg"));
        shell.setBackgroundMode(SWT.MOD1);



初始化Table:

table = new Table(group, SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE
| SWT.FULL_SELECTION | SWT.CHECK);
table.setHeaderVisible(true);
table.setLinesVisible(true);
table.setBounds(15,45,695, 290);
table.setItemCount(Itemcount);
table.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
        table.setFont(Display.getCurrent().getSystemFont());
        TableColumn[]  columns=new TableColumn[headers.length];
for (int i = 0; i < headers.length; i++) {
    columns[i] = new TableColumn(table, SWT.CENTER);
columns[i].setText(headers[i]);
columns[i].setResizable(Boolean.TRUE);
columns[i].setWidth(Width[i]);
}



Table的初始化基本就这些了,然后是推每个TableItem增加Listener,当有Selection事件时,根据行和列可以定位到每一个Cell,然后就可以处理事件,为一个Cell添加可编辑的方法是增加一个TableEditor:代码如下:

table.addListener(SWT.MouseDoubleClick, new Listener()

{
public void handleEvent(Event event) {
Point point = new Point(event.x, event.y);
final TableItem item = table.getItem(point);

if (item == null)
return;
for (int i = 0; i < columnsize; i++) {
Rectangle rectang = item.getBounds(i);
if (rectang.contains(point)) {
EDITABLECOLUMN = i;
final TableEditor editor = new TableEditor(
table);
Control oldEditor = editor.getEditor();
if (oldEditor != null)
oldEditor.dispose();
//添加Editor
final Text texteditor = new Text(table,
SWT.NONE);
texteditor.computeSize(SWT.DEFAULT, table
.getItemHeight());
editor.grabHorizontal = true;
editor.minimumHeight = texteditor.getSize().y;
editor.minimumWidth = texteditor.getSize().x;
editor.setEditor(texteditor, item,
EDITABLECOLUMN);
texteditor.setFont(new Font(display,"宋体",12, SWT.ITALIC));

texteditor.setText(item
.getText(EDITABLECOLUMN));
texteditor.forceFocus();
texteditor
.addModifyListener(new ModifyListener() {

//开始编辑的事件 public void modifyText(
ModifyEvent event) {
Text text = (Text) editor
.getEditor();
text.setForeground(display.getSystemColor(SWT.COLOR_RED));
editor.getItem().setText(
EDITABLECOLUMN,
text.getText());
}
});
                               
texteditor
.addFocusListener(new org.eclipse.swt.events.FocusAdapter() {
public void focusLost(
org.eclipse.swt.events.FocusEvent e) {

///这里写的是当编辑后失去焦点后做的处理
}



}  


这个编辑器贴代码不是很方便啊,就不再贴了,不过添加表格单元格编辑的基本的方法的代码已经在上面了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics