problem move row up or down in grid component

Button up = new Button();
up.addClickListener(new Button.ClickListener()
{ @Override public void buttonClick(Button.ClickEvent clickEvent)
{
final Object selected = ((Grid.SingleSelectionModel) mTable.getSelectionModel()).getSelectedRow();
int oldIndex = mTable.getContainerDataSource().indexOfId(selected);
mTable.getContainerDataSource().removeItem(selected);
mTable.getContainerDataSource().addItemAt(oldIndex-1, selected);
} });

my problem is when i push button up for example and introduce value null.

Any idea for resolve?

Hi!

Sorry, could you clarify your problem a bit? What exactly is null in your example situation? You don’t have any rows selected, or are trying to move the topmost row up, or something else entirely? You’d need to add special handling for those situations.

sorry for my bad English , what I want is my reorder grid rows , either up or down , to reorder up eg change the ranks of site but the row is blank rises above

try to create a drag and drop but with buttons as drag and drop does not work on component grid

I guess your row properties aren’t initialised automatically, so if you remove the item, and add it back to a different spot, you’ll need to reinitialise the contents or your columns will be blank. You might want to consider just reordering the container instead of removing the item temporarily, but it depends on your container how that would happen in practice and whether it allows it at all, can’t remember off the top of my head if I’ve ever tried it myself.

Also note that you can’t add components to a Grid, you’ll need a ButtonRenderer instead of a Button if this is something you want on every row.

My button is outside the grid , the operation would select a row in the grid , then capture the value of that row with the button and finally reorder row

Well, assuming you use Grid’s default IndexedContainer, consider trying out HierarchicalContainer instead. It has method moveAfterSibling(Object itemId, Object siblingId) that should come in handy. You might need to change how you initialise your Grid a bit – can’t use addColumn or addColumnProperty shortcuts if you don’t use the default container – but should be doable. I’m afraid I don’t have time to try it out myself at the moment, but I hope it will help you forward.

I’ll see how I do , thanks for the info , I can not change my indexed container , add much use and add column Column property

You could still use the container’s own methods for that, it’s only the Grid helper methods that break. I suspect you would need much more work to get it working with the default container than by converting to a different container that has item moving as a built-in property.

I had an idea , as the container indexed or beanitemcontainer can take your ArrayList inside, reordered the arraylist , I was debugging and did it well , gave me an error when deleting my grid and add the container , but it has to be easy to solve think I

Perfectly was solved first reorder arraylist , then delete all the items in the grid , I loop arraylist and will dynamically inserting values ​​and finally, keep selection after moving a row, thanks for everything, I leave this post in case anyone most he can serve. a greeting

Hi!

I have been trying to do similar thing, a file browser, for a few days.
Grid is bound to a
BeanItemContainer
object which is
bicnMain
in the code.

I can move row up and down like this:

private void butnMoveDownClickListener(Button.ClickEvent e) {
       
    Object rowSelected = this.gridMain.getSelectedRow();
    Object rowNext = this.bicnMain.nextItemId(rowSelected);

    if (rowNext != null) {

        this.bicnMain.removeItem(rowSelected);
        this.bicnMain.addItemAt(this.bicnMain.indexOfId(rowNext) + 1, rowSelected);

    }

}
private void butnMoveUpClickListener(Button.ClickEvent e) {

    Object rowSelected = this.gridMain.getSelectedRow();
    Object rowPrevious = this.bicnMain.prevItemId(rowSelected);

    if (rowPrevious != null) {

        this.bicnMain.removeItem(rowSelected);
        this.bicnMain.addItemAt(this.bicnMain.indexOfId(rowPrevious), rowSelected);

    }

}

Hello to All,
The solution of Sevket works perfect for me.

Thanks Alot
Elias