PagedTable Dynamic Row Insert

I am using PagedTable control and i am facing problem in dynamically adding row in the Table. When I add row dynamically, it is not reflected in the table UI, I have to either change the Items per Row (int the UI) or click on the column header to see the inserted row in the PagedTable.

Here is my code:

private void initUI()
{
final Action ACTION_ADD = new Action(“Add”);
final Action ACTION_EDIT = new Action(“Edit”);
final Action ACTION_CANCEL = new Action(“Cancel”);

	final Action[] SELECT_MENU_ACTIONS = new Action[]

{ ACTION_ADD, ACTION_EDIT,ACTION_CANCEL};
final Action NULL_MENU_ACTIONS = new Action
{ ACTION_ADD, ACTION_CANCEL};

	final PagedTable table = new PagedTable("");
	table.setWidth("100%");
	table.setSelectable(true);
	table.setMultiSelect(false);
	table.setImmediate(true); // react at once when something is selected
	
	table.setContainerDataSource(getDSStructure());
    table.setNullSelectionAllowed(false);
    table.setColumnCollapsingAllowed(true);

    // turn on column reordering and collapsing
    table.setColumnReorderingAllowed(true);
    table.setColumnCollapsingAllowed(true);
    
    // set column headers
    table.setColumnHeaders(new String[] {"ID","Name", "Short Name","Satus","ROD"});
    table.setVisibleColumns(new Object[] {"id", "name","shortName", "status","rod"});

    table.addActionHandler(new Action.Handler() 
    {
        public Action[] getActions(Object target, Object sender) 
        {
        	Item selectedItem = table.getContainerDataSource().getItem(target);
       	
        	if(selectedItem == null)
        		return NULL_MENU_ACTIONS;
        	else
        	{
        		return SELECT_MENU_ACTIONS;
        	}
        }

		public void handleAction(Action action, Object sender, Object target) 
		{
			// TODO Auto-generated method stub
			if (ACTION_ADD == action) 
            {
				try
				{
					showAddForm(table);[color=#130bf6]

// Takes input and inset a new row in the PagedTable
[/color]
}catch(Exception ex)
{
getWindow().showNotification(
“Error!”, ex.getMessage(),
Notification.TYPE_ERROR_MESSAGE);
}
return;
}
else if (ACTION_EDIT == action)
{
g_selectedItem = table.getContainerDataSource().getItem(target);
if(g_selectedItem == null)
return;

// showEditForm();
return;
}

		}
	});

    setWidth("100%");
    loadTableRecords(table.getContainerDataSource());
    addComponent(table);
    addComponent(table.createControls());

table.setPageLength(5); // Call this method only after calling table.createControls();

}

private IndexedContainer getDSStructure()
{
	IndexedContainer container = new IndexedContainer();
	
	container.addContainerProperty("id", String.class,null);
	container.addContainerProperty("name", String.class,null);
	container.addContainerProperty("shortName", String.class,null);
	container.addContainerProperty("status", String.class,null);
	container.addContainerProperty("rod", String.class,null);

	return container;
}

private void loadTableRecords(Indexed container ) throws Exception
{
	List<NBPojo>list = application.getNBBean().readAll();
	for(int i=0;i<list.size();i++)
    {
		addRow(container, list.get(i));
	}
}

private Item addRow(Indexed container, NBPojo data)
{
	SimpleDateFormat df = new SimpleDateFormat ("dd-MM-yyyy hh:mm:ss aa");

  	Item item = container.addItem(data.getID());
  	item.getItemProperty("id").setValue(data.getID());
  	item.getItemProperty("name").setValue(data.getName());
  	item.getItemProperty("shortName").setValue(data.getShortName());
  	item.getItemProperty("status").setValue(data.getFriendlyStatus());
  	item.getItemProperty("rod").setValue( df.format(data.getRod()));
  	
  	return item;
}

finally I am displaying a Window when user right clicks and chooses Add menu item and inside the form I am capturing data to be displayed in the PagedTable

showAddForm(final PagedTable table)
{


       public void buttonClick(ClickEvent event) 
        {

    		try
    		{
    			frm.commit();
    		}catch(Exception ex) 
    		{
    			return;
    		}

        	try
        	{
            	long id;
            	String name;
            	String shortName;
            	String status;

    			id = Long.parseLong((String)((TextField)frm.getField("id")).getValue());
    			name = (String)((TextField)frm.getField("name")).getValue();
    			shortName =  (String)((TextField)frm.getField("shortName")).getValue();
        
    			if(((String)cbStatus.getValue()).compareTo("Up") == 0)
    			{
    				status = "U";
    			}
    			else if(((String)cbStatus.getValue()).compareTo("Down") == 0)
    			{
    				status = "D";
    			}
    			else if(((String)cbStatus.getValue()).compareTo("Inactive") == 0)
    			{
    				status = "I";
    			}
    			else
    			{
    				throw new Exception("Invalid Status [" + (String)cbStatus.getValue() + "]

");
}

    			NBPojo dataObject =  addUser(id, name, shortName, status); // Insert into Database

    			
    			Item item = addRow(table.getContainerDataSource(), dataObject); [b]


// Insert into PagedTable - NOT DISPLAYED

[/b]
//table.setCurrentPage(table.getCurrentPage()); // Refresh Table with added Item
((Window) wnd.getParent()).removeWindow(wnd);

        	}catch(DuplicateEntityException dee)
        	{
        		dee.printStackTrace();
   			 	getWindow().showNotification(
		            "Duplicate!",  dee.getMessage(),
		            Notification.TYPE_ERROR_MESSAGE);	

        	}catch(Exception ex)
        	{
        		ex.printStackTrace();
      			 getWindow().showNotification(
  			            "Error!",  ex.getMessage(),
  			            Notification.TYPE_ERROR_MESSAGE);	
        		
        	}
        }
    });

}

Am I doing anything Wrong! or is there any workaround ?

I did workaround and not sure whether it is right way to do it

public static void refreshPagedTable(PagedTable table)
{

// Store the current Page Index in memory

int currentPage = table.getCurrentPage();

            [color=#f00c0c]

// Try to change to Some page which does not exist - Not getting any exception in PagedTable Implementation. Cool!
[/color]
table.setCurrentPage(table.getTotalAmountOfPages() + 1);

            [color=#f00c0c]

// Now change Back to the actual Page
[/color]
table.setCurrentPage(currentPage);

}

and I am calling refreshPagedTable(table) after inserting row in the table.