我有一个TableViewer
,当我按下回车键时,希望选择按下一个单元格,就像在MS Excel中一样。我使用以下CellNavigationStrategy
实现了我自己的findSelectedCell
。
public ViewerCell findSelectedCell(ColumnViewer viewer,
ViewerCell currentSelectedCell, Event event) {
if (event.type == ColumnViewerEditorActivationEvent.KEY_PRESSED) {
if (event.keyCode == SWT.CR
|| event.keyCode == SWT.KEYPAD_CR) {
ViewerCell nextCell = currentSelectedCell
.getNeighbor(ViewerCell.BELOW, false);
return nextCell;
}
}
return null;
}
只要我有ViewerCell.LEFT
或ViewerCell.RIGHT
,这项工作就会很好。
当我尝试ViewerCell.ABOVE
或ViewerCell.BELOW
nextCell
实际设置为。{
单元格在上方或下方,但在GUI中,选择保持在currentSelectedCell
。
findSelectedCell的API文档说:
退货:
下一个突出显示的单元格,如果是默认值则为null 实施。例如。这是不可能做出反应的 PAGE_DOWN请求
我不明白那句话是什么意思。任何人都可以向我说明为什么不能将选择设置为低于或高于某个单元格?
答案 0 :(得分:3)
当我尝试使用ViewerCell.ABOVE或ViewerCell.BELOW时,实际上是nextCell 设置为上方或下方的单元格,但在GUI中选择保持不变 currentSelectedCell。
您必须在KEY_PRESSED
事件后明确设置当前选择。现在有两种方法可以做到。
v.getTable().showColumn(v.getTable().getColumn(nextCell.getColumnIndex()));
其中是table viewer
个对象。现在这种方法通常适用于SWT.ARROW_DOWN
,SWT.ARROW_UP
等简单的键。但回车即SWT.CR
通常具有一些特殊含义,如提交表单,按复合键上的默认按钮等我没有彻底检查,但我的直觉表明它是由其他一些处理程序处理的,因此你会失去焦点。SWT.CR
,请使用此v.getTable().setSelection(((TableItem)nextCell.getItem()));
此外,您必须覆盖CellNavigationStrategy.isNavigationEvent()
,否则SWT.CR
和SWT.KEYPAD_CR
将被忽略。例如:
@Override
public boolean isNavigationEvent(ColumnViewer viewer, Event event) {
return event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR;
}
<小时/>
我不明白那句话是什么意思。
这意味着如果您打算使用JFace附带的CellNavigationStrategy
的默认实现,则无法处理SWT.PAGE_DOWN
键按下事件。原因是它没有在CellNavigationStrategy.isNavigationEvent()
中处理(有关详细信息,请参阅其实现)。
<小时/> 请参阅下面的完整工作代码:
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
public class CellNavTest {
public CellNavTest(Shell shell)
{
final TableViewer v = new TableViewer(shell, SWT.BORDER | SWT.FULL_SELECTION);
v.setContentProvider(new MyContentProvider());
TableViewerColumn column = new TableViewerColumn(v, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setText("Givenname");
column.getColumn().setMoveable(true);
column.setLabelProvider(new ColumnLabelProvider() {
public String getText(Object element) {
return ((Person) element).givenname;
}
});
column = new TableViewerColumn(v, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setText("Surname");
column.getColumn().setMoveable(true);
column.setLabelProvider(new ColumnLabelProvider() {
public String getText(Object element) {
return ((Person) element).surname;
}
});
column = new TableViewerColumn(v, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setText("E-Mail");
column.getColumn().setMoveable(true);
column.setLabelProvider(new ColumnLabelProvider() {
public String getText(Object element) {
return ((Person) element).email;
}
});
CellNavigationStrategy naviStrat = new CellNavigationStrategy()
{
@Override
public boolean isNavigationEvent(ColumnViewer viewer, Event event) {
return event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR;
}
public ViewerCell findSelectedCell(ColumnViewer viewer, ViewerCell currentSelectedCell, Event event)
{
if (event.type == ColumnViewerEditorActivationEvent.KEY_PRESSED) {
if (event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR)
{
ViewerCell nextCell = currentSelectedCell.getNeighbor(ViewerCell.BELOW, false);
if(nextCell != null)
{
/*
* START
* Shows the column. If the column is already showing in the receiver, this method simply returns.
* Otherwise, the columns are scrolled until the column is visible. So when you press enter it will just
* return the same column index and hence as per javadoc it will just return.
*/
//System.out.println(nextCell.getColumnIndex());
//v.getTable().showColumn(v.getTable().getColumn(nextCell.getColumnIndex()));
/*
* END
*/
if(nextCell.getItem() instanceof TableItem)
v.getTable().setSelection(((TableItem)nextCell.getItem()));
}
return nextCell;
}
}
return null;
}
};
new TableViewerFocusCellManager(v, new FocusCellOwnerDrawHighlighter(v), naviStrat);
Person[] model = createModel();
v.setInput(model);
v.getTable().setLinesVisible(true);
v.getTable().setHeaderVisible(true);
}
private Person[] createModel() {
Person[] elements = new Person[4];
elements[0] = new Person("Tom", "Schindl",
"tom.schindl@bestsolution.at", "M");
elements[1] = new Person("Boris", "Bokowski",
"Boris_Bokowski@ca.ibm.com","M");
elements[2] = new Person("Tod", "Creasey", "Tod_Creasey@ca.ibm.com","M");
elements[3] = new Person("Wayne", "Beaton", "wayne@eclipse.org","M");
return elements;
}
/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new CellNavTest(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
class MyContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
return (Person[]) inputElement;
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
class Person {
public String givenname;
public String surname;
public String email;
public String gender;
public Person(String givenname, String surname, String email, String gender) {
this.givenname = givenname;
this.surname = surname;
this.email = email;
this.gender = gender;
}
}