TableViewer 单元格编辑器不工作 - SWT
我正在尝试在 Eclipse SWT 中实现可编辑的表查看器。我认为到目前为止我已经完成了所有操作,问题是该表不可编辑 - 如果我单击任何行,则不会发生任何情况。
我已经为我的所有列注册了一些 CellEditors:
CellEditor[] editors = new CellEditor[columnNames.length];
editors[0] = new TextCellEditor(table);
//do the above for all columns
tableViewer.setCellEditors(editors);
然后为我的表指定一个单元格修饰符:
tableViewer.setCellModifier(new CellModifier(this));
CellModifier 类如下所示:
public class CellModifier implements ICellModifier{
private DBStructureView dbView;
public CellModifier(DBStructureView view){
super();
this.dbView = view;
}
@Override
public boolean canModify(Object element, String property) {
return true;
}
@Override
public Object getValue(Object element, String property) {
int columnIndex = dbView.getColumnNames().indexOf(property);
Object result = null;
AttributeNode node = (AttributeNode) element;
switch(columnIndex){
case 0://row id
result = node.getRow();
case 1://name
result = node.getName();
case 2://value
result = node.getValue();
default:
result = "unknown";
}
System.out.println(result);
return result;
}
@Override
public void modify(Object element, String property, Object value) {
int columnIndex = dbView.getColumnNames().indexOf(property);
TableItem item = (TableItem) element;
AttributeNode node = (AttributeNode)item.getData();
String valueString;
switch(columnIndex){
case 0:
valueString = ((String) value).trim();
node.setRow(valueString);
break;
case 1:
valueString = ((String) value).trim();
node.setName(valueString);
break;
case 2:
valueString = ((String) value).trim();
node.setValue(valueString);
break;
default:
break;
}
}
}
完成所有这些后,会出现什么问题?
提前致谢!
I am trying to implement an editable table viewer in Eclipse SWT. I think I've done everything ok until now, problem is the table is not editable - nothing happens if I click on any row.
I have registered some CellEditors with all my columns:
CellEditor[] editors = new CellEditor[columnNames.length];
editors[0] = new TextCellEditor(table);
//do the above for all columns
tableViewer.setCellEditors(editors);
and then I specify a cell modifier for my table:
tableViewer.setCellModifier(new CellModifier(this));
The CellModifier class looks like this:
public class CellModifier implements ICellModifier{
private DBStructureView dbView;
public CellModifier(DBStructureView view){
super();
this.dbView = view;
}
@Override
public boolean canModify(Object element, String property) {
return true;
}
@Override
public Object getValue(Object element, String property) {
int columnIndex = dbView.getColumnNames().indexOf(property);
Object result = null;
AttributeNode node = (AttributeNode) element;
switch(columnIndex){
case 0://row id
result = node.getRow();
case 1://name
result = node.getName();
case 2://value
result = node.getValue();
default:
result = "unknown";
}
System.out.println(result);
return result;
}
@Override
public void modify(Object element, String property, Object value) {
int columnIndex = dbView.getColumnNames().indexOf(property);
TableItem item = (TableItem) element;
AttributeNode node = (AttributeNode)item.getData();
String valueString;
switch(columnIndex){
case 0:
valueString = ((String) value).trim();
node.setRow(valueString);
break;
case 1:
valueString = ((String) value).trim();
node.setName(valueString);
break;
case 2:
valueString = ((String) value).trim();
node.setValue(valueString);
break;
default:
break;
}
}
}
Having done all this, what can go wrong?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现了错误,似乎我忘记设置列的属性。我补充道:
现在它工作正常,我可以编辑我的任何单元格!
I have found the bug, it seems I forgot to set the properties for the columns. I added:
and now it works fine, I can edit any of my cells!
getValue() 中的 switch 语句错过了
break;
在每种情况下,它总是将结果设置为“未知”。然后,如果我将您的代码与 示例 in JFace Snippets,看来您
在修改()中丢失了。
但这两个问题都没有解释为什么当您单击单元格时“没有发生任何事情”。由于我们没有您提供的运行片段,因此我们只能推测。你调试了吗?当您在 getValue() 和修改() 中设置断点并单击单元格时,是否会遇到断点?
更新:既然您写道,您的代码根本没有受到攻击,问题很可能出在您没有向我们展示的代码中。
请阅读 ColumnViewer.setCellEditor():
检查您的表是否具有该样式位。
The switch statement in your getValue() misses the
break;
in each case, it always sets result to "unknown".Then, if I compare your code with the example in JFace Snippets, it seems you are missing
in modify().
But both issues don't explain why "nothing happens", when you click the cell. Since we don't have a running snippet from you, we can only speculate. Did you debug? When you set breakpoints in getValue() and modify(), and click a cell, do you hit the breakpoints?
Update: Since you write, that your code doesn't get hit at all, the issue is most probably in the code you haven't shown us.
Please read the Javadoc of ColumnViewer.setCellEditor():
Check if your Table has that style bit.