Java 带有日期选择器字段的SWT表
这是一个显而易见的问题,但出于某种原因,我还没有找到一个有效的解决方案。我有一个3列的表,前两列是日期,第三列我使用微调器,因为我希望用户只输入数字。我需要所有字段都可以编辑,但是Java 带有日期选择器字段的SWT表,java,date,swt,editor,Java,Date,Swt,Editor,这是一个显而易见的问题,但出于某种原因,我还没有找到一个有效的解决方案。我有一个3列的表,前两列是日期,第三列我使用微调器,因为我希望用户只输入数字。我需要所有字段都可以编辑,但是 只有当字段处于焦点时,当单击该字段时,该字段才应可编辑 同一行中的其他字段不应编辑 当该字段失去焦点时,应返回“默认”模式 这些都是在可编辑表中所期望的正常情况。由于某些原因,我无法在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个编辑器。我想做的是获取所选列。我还不知道怎么做。如果我得到选定的列,那么我将能够仅为选定的单元格而不是整行创建编辑器
我还没有弄清楚在编辑器失去焦点后如何处理它。如果用户在表外单击,那么它显然已经发生了。但是,如果用户只是点击另一个手机怎么办?然后,我必须处理旧编辑器并创建一个新编辑器。要获取所选列,您可以为每个列调用
TableItem.getBounds(columnIndex)
,查看所选内容是否在返回的矩形内