这是一个显而易见的问题,但由于某种原因,我还没有找到可行的解决方案。我有一个包含 3 列的表格,前两个是日期,第三个是我使用 Spinner 因为我希望用户只输入数字。我需要所有字段都是可编辑的,但是
- 只有当它处于焦点时,当它被点击时,该字段才应该是可编辑的
- 同一行中的其他字段不应该是可编辑的
- 当字段失去焦点时,它应该回到“默认”模式
这些都是人们在可编辑表格中所期望的正常事物。由于某种原因,我无法在 SWT 中完成这项工作。现在我拥有的是:
- 我添加了一个新行,所有字段都是“默认”
- 我单击该行,所有字段都变为可编辑(日期字段变为组合框等)
- 行失去焦点后,仍可编辑
这是我现在拥有的代码,它基于我在互联网上找到的其他一些代码:
// this will happen when you click on the table
table.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Clean up any previous editor control
final TableEditor editor1 = new TableEditor(table);
// The editor must have the same size as the cell and must
// not be any smaller than 50 pixels.
editor1.horizontalAlignment = SWT.LEFT;
editor1.grabHorizontal = true;
editor1.minimumWidth = 50;
Control oldEditor = editor1.getEditor();
if (oldEditor != null)
oldEditor.dispose();
// Identify the selected row
TableItem item = (TableItem)e.item;
if (item == null) return;
// this is the editor for the first column
DateTime startDateTxt = new DateTime(table, SWT.BORDER | SWT.DROP_DOWN | SWT.LONG);
startDateTxt.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
DateTime text = (DateTime)editor1.getEditor();
editor1.getItem().setText(0, new LocalDate(text.getYear(), text.getMonth(), text.getDay()).toString());
}
});
editor1.setEditor(startDateTxt, item, 0);
// and now comes the editor for the 2nd column
//~~~~
// Clean up any previous editor control
final TableEditor editor2 = new TableEditor(table);
// The editor must have the same size as the cell and must
// not be any smaller than 50 pixels.
editor2.horizontalAlignment = SWT.LEFT;
editor2.grabHorizontal = true;
editor2.minimumWidth = 50;
oldEditor = editor2.getEditor();
if (oldEditor != null)
oldEditor.dispose();
// this is the editor for the second column
DateTime endDateTxt = new DateTime(table, SWT.BORDER | SWT.DROP_DOWN | SWT.LONG);
endDateTxt.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
DateTime text = (DateTime)editor2.getEditor();
editor2.getItem().setText(1, new LocalDate(text.getYear(), text.getMonth(), text.getDay()).toString());
}
});
editor2.setEditor(endDateTxt, item, 1);
//~~~~
// Clean up any previous editor control
final TableEditor editor3 = new TableEditor(table);
// The editor must have the same size as the cell and must
// not be any smaller than 50 pixels.
editor3.horizontalAlignment = SWT.LEFT;
editor3.grabHorizontal = true;
editor3.minimumWidth = 50;
oldEditor = editor3.getEditor();
if (oldEditor != null)
oldEditor.dispose();
// this is the editor for the third column
Spinner percentTxt = createNewSpinner(table, SWT.NONE);
percentTxt.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent me) {
Spinner text = (Spinner)editor3.getEditor();
editor3.getItem().setText(2, text.getText());
}
});
editor3.setEditor(percentTxt, item, 2);
//~~~~
}
});
如您所见,当用户单击表格时,我得到了当前行,并为该行添加了 3 个编辑器。我想做的是获取选定的列。我还没有找到如何做到这一点。如果我得到选定的列,那么我将能够为选定的单元格创建一个编辑器,而不是为整行创建一个编辑器。
我还没有弄清楚如何在失去焦点后处理编辑器。如果用户在表格之外点击,那么它显然已经发生了。但是如果用户只是点击另一个单元格呢?然后我将不得不处理旧编辑器并创建一个新编辑器。