Enter key to change the focus from one TextInputCell to another in a celltable.

528 views
Skip to first unread message

Nitin

unread,
Jul 25, 2012, 5:26:04 AM7/25/12
to google-we...@googlegroups.com

In the cell table to have tab flow working, I had to do the following: cellTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);

apart from changing the template of TextInputcell. This helped me in getting the tab flow working properly i.e. when I hit TAB the focus shifts to the next editable cell of the celltable.

But there is one more requirement in my project which says that the enter key should behave as the tab key and hence when we hit enter on a particular TextInputcell, the focus should move to the next TextInputcell of the celltable. Can anyone please help me with this?

I have tried lots of things to achieve this, but none is working. This is very urgent for the project. Please let me know, if you need more information on the issue.

Nitin

unread,
Jul 25, 2012, 12:23:05 PM7/25/12
to google-we...@googlegroups.com
cellTable.addCellPreviewHandler() did the trick for me. 

Now the issue is that, this method is getting called twice. Could someone explain the reasons for the handler getting called twice?

Thomas Broyer

unread,
Jul 25, 2012, 12:43:27 PM7/25/12
to google-we...@googlegroups.com


On Wednesday, July 25, 2012 6:23:05 PM UTC+2, Nitin wrote:
cellTable.addCellPreviewHandler() did the trick for me. 

Now the issue is that, this method is getting called twice. Could someone explain the reasons for the handler getting called twice?

When changing the focused element while processing a keyboard event, some browsers fire the event again on the newly focused element. That's probably the reason it's being called twice in your case.

Andrei

unread,
Jul 25, 2012, 4:16:12 PM7/25/12
to google-we...@googlegroups.com
It's called on all events in all cells, i.e. Blur, MouseOver, Click, etc. Just add a check for the type of event you want to process, like

if ("blur".equals(event.getNativeEvent().getType())) {
    // do something with a cell that loses focus
}
if ("focus".equals(event.getNativeEvent().getType())) {
    // do something with a cell that receives focus
}

Stefaan Vanderheyden

unread,
Sep 21, 2012, 5:09:08 PM9/21/12
to google-we...@googlegroups.com
Here is some code you might also find applicable to your use case:

    private void focusOnNext(final int currentRow, final int currentCol) {
        // Scan remaining cells in table for a form element to focus on.
        for (int r = currentRow; r < this.pricingTable.getRowCount(); r++) {
            final NodeList<TableCellElement> rowCells =
                    this.pricingTable.getRowElement(r).getCells();
            for (int c = 0; c < rowCells.getLength(); c++) {
                if ((r == currentRow) && (c <= currentCol)) {
                    // don't process these cells since we would be moving the
                    // focus backwards!
                } else {
                    if (rowCells.getItem(c).getElementsByTagName("input")
                            .getItem(0) != null) {
                        rowCells.getItem(c).getElementsByTagName("input")
                                .getItem(0).focus();
                        return;
                    }
                    if (rowCells.getItem(c).getElementsByTagName("select")
                            .getItem(0) != null) {
                        rowCells.getItem(c).getElementsByTagName("select")
                                .getItem(0).focus();
                        return;
                    }
                    if (rowCells.getItem(c).getElementsByTagName("textarea")
                            .getItem(0) != null) {
                        rowCells.getItem(c).getElementsByTagName("textarea")
                                .getItem(0).focus();
                        return;
                    }
                }
            }
        }
    }

I call this method in my FieldUpdater after redrawing the row on a value change.  It works very nicely!

Stefaan Vanderheyden

unread,
Sep 21, 2012, 5:11:10 PM9/21/12
to google-we...@googlegroups.com
Sorry, I forgot to mention:

You get the currentRow from the index value provided in the field updater and the currentCol by querying the celltable for the updated column's index (cellTable.getColumnIndex(updatedCol))...

ren...@ipay.co.za

unread,
Mar 8, 2019, 4:47:20 AM3/8/19
to GWT Users
You are a STAR Stefaan!! Thank you so much. After adding validation to the inputCell it was a real problem to get the tabbing sorted again. Thank you!
Reply all
Reply to author
Forward
0 new messages