Adding beanItem from Form's datasource to Table's datasource?

Hi!

I have no idea is this stupid question or not, but for me, it’s important :slight_smile:

I have a page divided to two horizontal panels. In left side I’ve got table called staffTable. In right side I’ve got a form, called addingUserForm. I am trying to add users/staff to the table using that form.

Table’s data source is BeanItemContainer:


 final BeanItemContainer<Staff> container = new BeanItemContainer<Staff>(Staff.class);
		
staffTable = new Table("Adding staff", container);
staffTable.setContainerDataSource(container);
staffTable.setVisibleColumns(new String[] {"firstName","lastName", "userName"});
staffTable.setColumnHeader("firstName", "First Name");
staffTable.setColumnHeader("lastName", "Last Name");

Form’s datasource is BeanItem


staffItem = new BeanItem<Staff>(staff);
staffItem.addItemProperty("company", new NestedMethodProperty(staff, "company.companyName"));
....		
addingUserForm = new Form();
addingUserForm.setItemDataSource(staffItem,visibleProps);

When I press the button my bean is populated, so staffItem gets the data:


Button apply = new Button("Add", new Button.ClickListener() {
        	
	public void buttonClick(ClickEvent event) {
                try {
                	
                	addingUserForm.commit();
                        // Adds a new staff to table's dataSourceContainer
                        container.addItem(staffItem);//?????


And the question is:

When I am adding new staff I am always using a same instance of staffItem, so I can’t add a new one at all, I just change the old object. Size of the container is always 1. Is there some kind of convenient way to do that?

container.addItem(new BeanItem((Staff)staffItem.getBean()));----> This is NOT working
container.addBean(new Staff(copying everything from BeanItem to staff)) // new constructor, sounds bad

Thanks,
Sami

Hi Sami,

Try with this:

ontainer.addItem((Staff)staffItem.getBean())

It should work.

Hope this helps.

Javi

Hi and thanks!

It does exactly the same because in my case there is the same staffItem-instance, so it change the the same object and not make the new one.


Image after adding first item to container


Img2 after second bean added to container

Maybe I have to design the whole code again, because I can’t fix that, I just need NEW object/beanitem from the staffitem :frowning:


QUESTION2

I have a layout what includes splitPanel (Table + Form). I recognized as well that t.setSelectedTab(tabLayout4);
t.requestRepaintAll(); is NOT working. When the user press the “everything done” in that page, user is not transferred to the next tabsheet called tabLayout4. It’s working in everywhere else but not in that case, have you idea for that? The “everything done”-button is in layout what is inside of the panel.

Links to pictures:
http://aijaa.com/v.php?i=000769068337.png
http://aijaa.com/v.php?i=002719068338.png

Thanks a lot for helping me, I really appreciate it!
Sami

Hi

This is what i suggest to resolve your problem :

If you change your listener a little bit, I think it would do it.

Button apply = new Button("Add", new Button.ClickListener() {
            
    public void buttonClick(ClickEvent event) {
                try {
                   
                    addingUserForm.commit();
                     container.add(staffItem);

                     // Rebind your form with a new instance of BeanItem
                     staffItem = new BeanItem<Staff>(new Staff()); 
                     addingUserForm.setItemDataSource(staffItem);
                    } 
                    

Regards,

Éric

JIHUUUUUU! That was it :slight_smile: Thanks thanks thanks! Have you an idea to my second question.

I implemented a new form-class called StaffForm, because my file started to be horrible and too big etc. I used your logic and that was it, I am so happy now :slight_smile:
BTW, how big work is to change the container type to SQLContainer? Finally I have to do it. I have a 5 tabsheets and I am creating a new company with staff, branches, basic infos etc. Now I am just collecting data to CompanyBean and after the last tab everything is going to the database when user press save-button. Is that wise idea or should I add everything pieve by piece to the DB?


commit();
			tableContainer.addBean(staffItem.getBean());
			staffItem = new BeanItem<Staff>(new Staff()); 
			staffItem = addPropertiesToStaffItem(staffItem); //add nested properties to staffItem
			this.setItemDataSource(staffItem,visibleProps);

Sami

I never use SQL Container, so I can’t tell.

For your question #2 : I’m not sure I understand perfectly. But if you want the user to be redirected to a specific tab, you can do something like this :


button.addClickListener(){
 // Tabsheet is a reference to the tabsheet where your layout in attach
 tabSheet.setSelectedTab(//here you insert the index of the tab you want to select)
}

Not tested it, but I think it would work.