Regarding grid and the form field binding

Hi,

I have a combobox in my form.On click on the items selection in the combobox, I am populating the form with another comboboxes.And the result of all my sub comboboxes, i am storing into the set collection.The set object I am trying to place into the grid column.

Below is the code snipped for my parent combobox.

agrupación.addValueChangeListener(new ValueChangeListener() {

@Override
public void valueChange(ValueChangeEvent event) {
agruset = (Set) event.getProperty().getValue();
System.out.println(“event.getProperty().getValue() ----” + event.getProperty().getValue());
reglagrupo.setAgrupación(agruset);

if(agruset.contains(“EDAD”)) {
edad.setVisible(true);
edad.addValueChangeListener(new ValueChangeListener() {

@Override
public void valueChange(ValueChangeEvent event) {
for (String str : edadset) {
if (valuesset.contains(str)) {
if (str == event.getProperty().getValue().toString()) {
continue;
} else {
valuesset.remove(str);
valuesset.add(event.getProperty().getValue().toString());
}
} else {
valuesset.add(event.getProperty().getValue().toString());
}
}

}
});
}
else {
for (String str : edadset) {
if (valuesset.contains(str)) {
valuesset.remove(str);
}
}
edad.setVisible(false);
}
if(agruset.contains(“Profession”)) {
profession.setVisible(true);
}
else {
for (String str : profset) {
if (valuesset.contains(str)) {
valuesset.remove(str);
}
}
profession.setVisible(false);
}
if(agruset.contains(“REGIMEN”)) {
regimen.setVisible(true);
}
else {
for (String str : regset) {
if (valuesset.contains(str)) {
valuesset.remove(str);
}
}
regimen.setVisible(false);
}
//values.addItems(valuesset);

reglagrupo.setValues(valuesset);

}

});

And here is m pojo class.

private Long id;
private long IDFactorRegla;
private Set Agrupación;
private Set values;
private Double Tasa;

I am able to insert one record in the grid and all the manupulation like modifying, delete is working fine.

but when i am trying to insert the second record, My all the previous records are also updating for my subcombobox(which is coming from my parent combobox based on the number of items seclection).

please help me out for this i am new to vaadin :

Can you please post the complete source for your example, since all the variable declarations are missing. I was not able to create a runnable class with only the given code.

Sounds like binding problem to me.

Do you bind all the records to the same Bean ? Idealy show us the project on github or something :slight_smile:

@Stefan : Thank you for your reply. Pleas find below the complete source of classes

POJO Class :
private Long id;
private long IDFactorRegla;
private Set Agrupación;
private Set values;
private Double Tasa;
// getter and setter
//tostring and clone override

ReglaGroupForm.class :

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.ama.caracteristicas.AmaUI;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.event.ShortcutAction;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.ValoTheme;

public class ReglaGroupForm extends FormLayout {

TextField IDFactorRegla = new TextField(“IDFACTORREGLA”);
ListSelect agrupación = new ListSelect(“Agrupación”);
ListSelect values = new ListSelect(“Values”);
//ComboBox values = new ComboBox(“Values”);
//TextField tasa1 = new TextField(“Tasa1”);
TextField tasa = new TextField(“Tasa”);

Set agruset = new HashSet();
Set valuesset = new HashSet();
Set profset = new HashSet();
Set edadset = new HashSet();
Set regset = new HashSet();

Set set06 = new HashSet();
String s1 = “”;
String s2 = “”;
String s3 = “”;

Window subWindow = new Window(“Regla Grupo Mantenimiento”);

ComboBox profession = new ComboBox(“Profession”);
ComboBox edad = new ComboBox(“Edad”);
ComboBox regimen = new ComboBox(“Regimen”);

Button save = new Button(“Guardar”, this::save);

Button delete = new Button(“Borrar”, this::delete);

// Easily bind forms to beans and manage validation and buffering
// com.vaadin.data.fieldgroup.BeanFieldGroup formFieldBindings;
ReglaGrupo reglagrupo;

// Easily bind forms to beans and manage validation and buffering
BeanFieldGroup formFieldBindings;

public ReglaGroupForm() {
valuesset.clear();
profset.clear();
edadset.clear();
regset.clear();
configureComponents();
buildLayout();
}

private void configureComponents() {

profession.setVisible(false);
edad.setVisible(false);
regimen.setVisible(false);
/*

  • Highlight primary actions.
  • With Vaadin built-in styles you can highlight the primary save button and
  • give it a keyboard shortcut for a better UX.
    */
    save.setStyleName(ValoTheme.BUTTON_PRIMARY);
    save.setClickShortcut(ShortcutAction.KeyCode.ENTER);
    delete.setStyleName(ValoTheme.BUTTON_PRIMARY);
    delete.setClickShortcut(ShortcutAction.KeyCode.ENTER);
    setVisible(false);

}

private void buildLayout() {

setSizeUndefined();
setMargin(true);

HorizontalLayout actions = new HorizontalLayout(save, delete);
actions.setSpacing(true);
agrupación.addItems(“Profession”,“EDAD”, “REGIMEN”);

for (Iterator iterator = (Iterator) agrupación.getItemIds().iterator(); iterator.hasNext():wink: {
String string = iterator.next();
set06.add(string);

}
Iterator it = set06.iterator();
int i=0;
while(it.hasNext()){
String str=it.next();
//ComboBox cb[i]
= new ComboBox(str);

i=i+1;
}
agrupación.setNullSelectionAllowed(false);
agrupación.setRows(5);
agrupación.setMultiSelect(true);
profession.addItems(“prof1”, “prof2”, “prof3”);

for (Iterator iterator = (Iterator) profession.getItemIds().iterator(); iterator.hasNext():wink: {
String string = iterator.next();
profset.add(string);

}

profession.addValueChangeListener(new ValueChangeListener() {

@Override
public void valueChange(ValueChangeEvent event) {

for (String str : profset) {
if (valuesset.contains(str)) {
if (str == event.getProperty().getValue().toString()) {
continue;
} else {
valuesset.remove(str);
valuesset.add(event.getProperty().getValue().toString());
}
} else {
valuesset.add(event.getProperty().getValue().toString());
}
}

}
});

edad.addItems(“edad1”, “edad2”, “edad3”);
for (Iterator iterator = (Iterator) edad.getItemIds().iterator(); iterator.hasNext():wink: {
String string = iterator.next();
edadset.add(string);

}

regimen.addItems(“regimen1”, “regimen2”, “regimen3”);
for (Iterator iterator = (Iterator) regimen.getItemIds().iterator(); iterator.hasNext():wink: {
String string = iterator.next();
regset.add(string);

}

regimen.addValueChangeListener(new ValueChangeListener() {

@Override
public void valueChange(ValueChangeEvent event) {

for (String str : regset) {
if (valuesset.contains(str)) {
if (str == event.getProperty().getValue().toString()) {
continue;
} else {
valuesset.remove(str);
valuesset.add(event.getProperty().getValue().toString());
}
} else {
valuesset.add(event.getProperty().getValue().toString());
}
}
}
});

agrupación.addValueChangeListener(new ValueChangeListener() {

@Override
public void valueChange(ValueChangeEvent event) {
agruset = (Set) event.getProperty().getValue();
System.out.println(“event.getProperty().getValue() ----” + event.getProperty().getValue());
reglagrupo.setAgrupación(agruset);

if(agruset.contains(“EDAD”)) {
edad.setVisible(true);
edad.addValueChangeListener(new ValueChangeListener() {

@Override
public void valueChange(ValueChangeEvent event) {
for (String str : edadset) {
if (valuesset.contains(str)) {
if (str == event.getProperty().getValue().toString()) {
continue;
} else {
valuesset.remove(str);
valuesset.add(event.getProperty().getValue().toString());
}
} else {
valuesset.add(event.getProperty().getValue().toString());
}
}

}
});
}
else {
for (String str : edadset) {
if (valuesset.contains(str)) {
valuesset.remove(str);
}
}
edad.setVisible(false);
}
if(agruset.contains(“Profession”)) {
profession.setVisible(true);
}
else {
for (String str : profset) {
if (valuesset.contains(str)) {
valuesset.remove(str);
}
}
profession.setVisible(false);
}
if(agruset.contains(“REGIMEN”)) {
regimen.setVisible(true);
}
else {
for (String str : regset) {
if (valuesset.contains(str)) {
valuesset.remove(str);
}
}
regimen.setVisible(false);
}
//values.addItems(valuesset);

reglagrupo.setValues(valuesset);

}

});

//tasa1.setValue(valuesset.toString());
agrupación.setImmediate(true);

setSizeUndefined();
setMargin(true);

agrupación.setNullSelectionAllowed(false);
///agrupación.setRows(5);
agrupación.setMultiSelect(true);

addComponents(IDFactorRegla, agrupación, profession, edad, regimen,tasa, actions);

}

public void save(Button.ClickEvent event) {

try {
// Commit the fields from UI to DAO
formFieldBindings.commit();

// Save DAO to backend with direct synchronous service API
getUI().reglagruposervice.save(reglagrupo);

getUI().refreshContactsGrupoRegla();

} catch (FieldGroup.CommitException e) {
// Validation exceptions could be shown here
}

}

public void edit(ReglaGrupo contact) {
//Notification.show(" items Clieckeddddddddddddd");
this.reglagrupo = contact;

if (contact != null) {

// Bind the properties of the contact POJO to fiels in this form
formFieldBindings = BeanFieldGroup.bindFieldsBuffered(contact, this);
IDFactorRegla.setEnabled(false);
// tasa1.setEnabled(false);

}
setVisible(contact != null);
}

public void delete(Button.ClickEvent event) {

try {

// Commit the fields from UI to DAO
formFieldBindings.commit();
// Create a sub-window and set the content
VerticalLayout subContent1 = new VerticalLayout();
HorizontalLayout subContent = new HorizontalLayout();
subWindow.setContent(subContent1);
subContent.setSpacing(true);
Button yesbtn = new Button(“Yes”);
Button nobtn = new Button(“No”);
// Put some components in it
subContent1.addComponent(new Label(“Are you sure you want to delete this Regla?”));
subContent1.addComponent(subContent);
subContent.addComponent(yesbtn);
subContent.addComponent(nobtn);

// Center it in the browser window
subWindow.center();
getUI().addWindow(subWindow);
yesbtn.addClickListener(e → deleteyesfn());
nobtn.addClickListener(e → deletenofn());

// Save DAO to backend with direct synchronous service API
// getUI().grouposervice.delete(groupo);

// getUI().refreshGroupo();

// Commit the fields from UI to DAO

} catch (FieldGroup.CommitException e) {
// Validation exceptions could be shown here
}

}

private void deletenofn() {
getUI().removeWindow(subWindow);

}

private void deleteyesfn() {
// Save DAO to backend with direct synchronous service API
getUI().reglagruposervice.delete(reglagrupo);

getUI().refreshContactsGrupoRegla();
getUI().removeWindow(subWindow);
}

@Override
public AmaUI getUI() {
return (AmaUI) super.getUI();
}
}

AmaUI.class :
//This is a very big class having any grids. So I am posting only the concerned grid

grid4.removeAllColumns();
grid4.addColumn(“IDFactorRegla”).setHeaderCaption(“IDFACTORREGLA”);
grid4.addColumn(“agrupación”).setHeaderCaption(“Agrupación”);
grid4.addColumn(“values”).setHeaderCaption(“Values”);
grid4.addColumn(“tasa”).setHeaderCaption(“Tasa”);

Window subsubWindow = new Window(“Regla Grupo Mantenimiento”);

grid4.setWidth(75, Unit.PERCENTAGE);
grid4.setHeight(75, Unit.PERCENTAGE);

VerticalLayout layout = new VerticalLayout();

layout.addComponent(newvalue1);
layout.addComponent(grid4);
HorizontalLayout mainLayout = new HorizontalLayout(layout, reglagroupform);

layout.setSpacing(true);
layout.setEnabled(true);
subsubWindow.setContent(mainLayout);

// setContent(layout);
subsubWindow.setModal(true);
getUI().addWindow(subsubWindow);
//getUI().addWindow(subsubWindow);
getUI().setVisible(true);
grid4.setSelectionMode(Grid.SelectionMode.MULTI);

refreshContactsGrupoRegla();

grid4.addSelectionListener(event1 → {

// Notification.show(" items selected");

for (Object itemId : grid4.getSelectedRows()) {

reglagroupform.edit((ReglaGrupo) itemId);

}

// Notification.show(selected.size() + " items selected");
});

// left.setComponentAlignment(grid1, Alignment.BOTTOM_RIGHT);
// getUI().addWindow(window);

// getUI().addWindow(new Window());
}
});

@ Vilius : Yes I am binding the all the records to the same bean. I have attched my classes to my previous thread.
(A reply to stefan post)

i could not make your Code runing, there are a few referces from clases that i do not have, so here is theoretical IDea.

as i understood, code works fine with 1 item, but when you add second item, the Item nr 1 starts to get his values also ?

Is it possibole, that by any chance bouth items are referencing to the same ValueProvider ?

You know, java gives often reference, not value. Maybe something with that ?

I am temptet to make it all nice and clean in Vaadin8 as small example, but i’m not sure that this would help you :frowning: