質問

So I have a cell table with click event selection model working fine. I later found out you can press UP and DOWN arrows to get the highlighted row to change, but the awful thing is you have to press Space for it to actually call the SelectionChangeEvent. I am trying to cheat my way a little, by catching the UP and DOWN events and firing the SPACE event. Sadly it doesn't work :( Here is my code any help would be appreciated!

        table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
        table.sinkEvents(Event.KEYUP);
        table.sinkEvents(Event.KEYDOWN);
        table.sinkEvents(32);
        table.addHandler(new KeyUpHandler(){
            @Override
            public void onKeyUp(KeyUpEvent event)
            {
                System.out.println(event.getNativeKeyCode());
                if(event.getNativeEvent().getKeyCode() == 40)
                {
                    // down is pressed
                    int i = rows.getFilterList().indexOf(selectionModel.getLastSelectedObject())+1;
                    if(i >= 0 && i < rows.getFilterList().size())
                    {
//                      selectionModel.setSelected(selectionModel.getLastSelectedObject(), false);
//                      selectionModel.setSelected(rows.getFilterList().get(i), true);
//                      SelectionChangeEvent.fire(selectionModel);
                        System.out.println("firing native event space");
                        DomEvent.fireNativeEvent(Document.get().createKeyUpEvent(false, false, false, false, 32), table);
                    }
                }
                else if(event.getNativeEvent().getKeyCode() == 38)
                {
                    // up is pressed
                    int i = rows.getFilterList().indexOf(selectionModel.getLastSelectedObject())-1;
                    if(i >= 0 && i < rows.getFilterList().size())
                    {
//                      selectionModel.setSelected(selectionModel.getLastSelectedObject(), false);
//                      selectionModel.setSelected(rows.getFilterList().get(i), true);
//                      SelectionChangeEvent.fire(selectionModel);
                        System.out.println("firing native event space");
                        DomEvent.fireNativeEvent(Document.get().createKeyUpEvent(false, false, false, false, 32), table);
                    }
                }
            }

        }, KeyUpEvent.getType());

32 is assumingly the NativeEvent for space, my console prints something like:

40
firing native event space
32

so assumingly the event type 32 is being called for the object table.

I check if the object is selected, because on the right hand side of the screen I have additional information being pulled out from a list, since the cell table doesn't show all the information. I want it so when I press UP and DOWN the RHS information changes and I dont have to press SPACE to prompt the info change

役に立ちましたか?

解決

Ideally you would poke into the selection internals. Specifically the DefaultKeyboardSelectionHandler is the default implementation of keyboard navigation and the DefaultSelectionEventManager is the default implementation of selection actions using spacebar/clicks (they are both CellPreviewEvent.Handlers).

Anyway, you can force the keyboard selection to be bound to the underlying SelectionModel by using setKeyboardSelectionPolicy(KeyboardSelectionPolicy.BOUND_TO_SELECTION). It should be fine for your use case. Much like what is done for the CellList showcase sample (the selection API is the same across cell widgets).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top