gpt4 book ai didi

java - 如何在 SWT TableViewer 中使用 Enter 键向下导航

转载 作者:行者123 更新时间:2023-12-04 06:14:20 25 4
gpt4 key购买 nike

我有一个 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.ABOVEViewerCell.BELOW nextCell实际上设置为
上方或下方的单元格,但在 GUI 中,选择保持在 currentSelectedCell .

findSelectedCell 的 API 文档说:

Returns:

the cell which is highlighted next or null if the default implementation is taken. E.g. it's fairly impossible to react on PAGE_DOWN requests



我不明白这句话是什么意思。任何人都可以向我解释为什么不能将选择设置为下方或上方的单元格?

最佳答案

When I try ViewerCell.ABOVE or ViewerCell.BELOW nextCell is actually set to the cell above or below, but in the GUI the selection stays at 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.CRSWT.KEYPAD_CR将是 忽略。 例如:
    @Override
    public boolean isNavigationEvent(ColumnViewer viewer, Event event) {
    return event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR;
    }

    I do not understand what that sentence means.



    这意味着如果您打算使用 CellNavigationStrategy 的默认实现,它是随 JFace 一起提供的,则无法处理 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;
    }

    }

    关于java - 如何在 SWT TableViewer 中使用 Enter 键向下导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7444171/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com