Vaadin7 - Grid disable unselecting - java

How to disable unselecting Grid row in Vaadin 7, but with permission to select another row using keyboard or mouse click?
Grid grid = new Grid(container);
grid.setSelectionMode(Grid.SelectionMode.SINGLE);
For example this is possible for older Table component - SO answer. But I widely use Grid so I want use it also in this case.

I found one interesting solution, but unfortunately not perfect.
To prevent deselect row we could write a SelectionListener and put there some logic:
grid.setSelectionMode(Grid.SelectionMode.SINGLE);
grid.addSelectionListener(event -> {
Set<Object> selected = event.getSelected();
if (selected == null || selected.isEmpty()) {
Set<Object> removed = event.getRemoved();
removed.stream().filter(Objects::nonNull).forEach(someGrid::select);
}
});
So assuming single selection mode, if current selection is empty, then previous selected row should be selected again. But if current selection isn't empty it means that somebody select another row - this doesn't require any action.
It is cool but not enough - every click (selection) cause http call and network transmission. This is disadvantage.

In Vaadin 8 you may use:
grid.setSelectionMode(SINGLE);
((SingleSelectionModel) grid.getSelectionModel()).setDeselectAllowed(false);

Related

Vaadin 8 Grid - perform action on changing (row) focus - GridFastNavigation

when searching for anything related to a Vaadin grid focus listener or similar, they keep pointing me to the GridFastNavigation add-on... and I tried it...
The only thing I want to do is to perform a simple action (two lines of code - refresh a preview) when the (row) focus in the Grid changes, i.e. when up or down keys are pressed. Nothing editable there, the grid is used only for display.
We extended the grid, and like in the demo project, I initialize the navigation stuff in the constructor of our grid. This is the code I currently have there
FastNavigation<M> nav = new FastNavigation<>(this, true, false);
nav.setChangeColumnAfterLastRow(true);
nav.addRowFocusListener(event -> {
if (event.getRow() >= 0) {
M model = (M) event.getItem();
refreshDetailLayout(model);
}
});
This looks very straightforward, but nothing happens, when navigating the grid. I tried debugging, and the only thing I could find out is that the code in the listener doesn't even get executed.
Are there any certain prerequisites for this add-on to work? Are there any known restrictions like other listeners right on the grid (such as ItemClick- or ShortcutListener) interfering with it?
I'm currently at Vaadin 8.4.5 and GridFastNavigation 2.3.5

Avoid vaadin grid automatic horizontal scroll after click

I'm using vaadin 8.1 in my project.
I needed to give an extra large width to a grid column (the latest at the right), and when user clicks in one field of this extra large column, vaadin move horizontal scroll automatically at the end of the grid. I would like stop this vaadin behaviour. I researched and I tried different things but I didn't find anything,
How can I do it?
Thank you,
Best regards
There is no API in Vaadin to turn it off (You can find feature request here: https://github.com/vaadin/framework/issues/7667). There is one trick you could try, namely disable pointer events in Grid cells. It is a bit harsh method, since it will disable also selection and item click.
In your code
grid.setStyleGenerator(item -> { return "disable-events";});
And your theme
.disable-events {
pointer-events: none;
}
It is possible to add the style generator also for one Column only, which could be option to you as well

Vaadin: How to focus on first row after updating the grid in Vaadin?

i'm building a Vaadin application with a Grid table within. The application reloads and updates every minute the table. SelectionMode.NONE and v-grid-row-focused are used. Unfortunately, the focus on the first row in the grid table disappears after each data refreshing.After some analysis I found out that after each table reloading the grid returns to v-grid-cell-focused-mode and only after pressing the arraydown-button for scrolling down the row focus it changes to the v-grid-row-focused-mode. Is there a way to completely disable v-grid-cell-focused-mode?
to get the 1st itemId (not Item) of a Table or Grid, you must go through the Container.firstItemId
Tables has an assessor method to this, but Grid doesn't
The solution proposed by Morfic won't work always, because the integer autogenerated by the containers when you use addItem() with no parameters (Indexed and Hierarchical) is incremental, if u clean/remove all/some items, the index will keep growing (not reset to 0) AND the index STARTS from 1, not 0 (if you check the source code), or if your implementation has the capacity to remove Items from the Container and the 1st one is removed, the ZERO index won't exist anymore
grid.select(grid.getContainerDataSource().getIdByIndex(0));
There is no method in Grid to set focused cell from server side. However I added such method setFocusedCell(row,col) in GridFastNavigation add-on extension for Vaadin 8.
https://vaadin.com/directory/component/gridfastnavigation-add-on

Suppress default double-click event in SWT.CHECK Table

I am using a SWT Table styled with SWT.CHECK and SWT.FULL_SELECTION. I added a MouseListener to fill an output group, when a user double-clicks a table row. However, a double-click will also toggle the row's checkbox. Though I suppose this is intended behaviour of the target platform (Windows), I want to prevent the checkbox from toggling.
Is it possible to not make the checkbox toggle on double-click?
I figured out, that the Table implementation I was using added a SelectionListener that toggled the checkbox.
When I removed it, everything worked as expected:
Listener[] listeners = table.getListeners(SWT.Selection);
if (listeners.length > 0) {
TypedListener typedListener = (TypedListener)listeners[0];
SelectionListener selectionListener = (SelectionListener)typedListener.getEventListener();
table.removeSelectionListener(selectionListener);
}
Ain't a clean solution, but since I know that the object only has one SelectionListener at that time, it seems legitimate to remove it this way.

Shifting the focus from one cell to another in Jtable on tab press

Summary:
In my desktop application i load one Jtable and when in edit mode if i press tab i need the focus of the cell on to the next cell.
Problem:
When i am editing the value of a cell and then when i press Tab the focus is lost. I did some search on the net and i found that it happens because on each Tab press the Jtable reloads itself.
Possible Solution
One solution that i was thinking of is to get the indices of the cell i am working in, same it in a global variable and then on Tab press i can get the indices of the next cell and set focus on that cell.
Somehow it didn't work.
Please suggest.
Thanks in advance..
Off the top of my head, I think we overcame this with a custom keystroke implementation in the tabes InputMap & ActionMap.
The implementation we use allows us to perform "continuous" editing, that is, when the user presses enter or tab, we move to the next editable cell and start editing
InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
ActionMap am = table.getActionMap();
KeyStroke tabKey = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
Action moveNextCellAction = am.get(im.get(tabKey));
ContinousEditAction continousEditAction = new ContinousEditAction(table, moveNextCellAction);
im.put(tabKey, "Action.tab");
am.put("Action.tab", continousEditAction);
The ContinousEditAction is responsible for finding the next editable cell. Basically when the action is fired, you take stock of the current cell via JTable.getEditingRow & JTable.getEditingColumn methods (you also want to check that the table is edit mode via JTable.isEditing, otherwise you need to use JTable.getSelectedRow & JTable.getSelectedColumn - in fact you might get away with doing just this, but this is how I approached the problem).
From there, you want to walk the cells until you find a cell that is editable.
Basically, you want to check to the end of the current row, then move to the next until no more rows exist, depending on what you want to do, you may choose to loop back around to the start of the table (cell 0x0) and walk it until you reach your current position.
Be careful, you can end up in a continuous loop if you're not careful :P.
If you don't find any editable cells, you may simply wish to select the next available cell using JTable.setRowSelectionInterval & JTable.setRowSelectionInterval, other wise you can call JTable.editCellAt(nextRow, nextCol)
But this all comes down to what it is you want to achieve.
Also, you can apply the same idea to the enter key ;)
Normally tab works in jTable once getting the focus .If you want to edit next cell by pressing Tab key give the following code in the key release event of jTable.
if (evt.getKeyCode() == 9) {
jTable1.editCellAt(nextRowIndex, nextColumnIndex);
}

Categories

Resources