I had a grid with textfield and checkbox and amount as its columns and when
enter amount in textfield out of grid and
when I click the header checkbox the amount should be compared with textfieldvalue and check only particular checkboxes.its working fine, but if I have 1000 rows for suppose user entered amount is equal to sum of 2 columns amount, only particular checkboxes are checked and amount is stored in that column textfield.The problem is when i scroll down the grid and again scroll up the checkboxes are unchecked and values in textfields are empty. Here is my code
[code]
final VerticalLayout layout = new VerticalLayout();
final HorizontalLayout hlayout = new HorizontalLayout();
final TextField txtamount = new TextField(“Amount”);
Label lbl = new Label();
/**
- adding components that need to display horizontally to horizontal layout
/
hlayout.addComponents(txtamount, lbl);
/*
*Create list of person type where person is a class.
adding values to list
/
List people = new ArrayList();
for (int i = 0; i < 100; i++) {
people.add(new Person(“Galileo Galilei”, 1564, 1000));
}
people.add(new Person(“Galileo Galilei”, 1564, 2000));
CheckBox CheckBox1 = new CheckBox(“All”);
CheckBox1.setValue(false);
/ - creating grid of person type,
- set list items to grid
- use addColumns to get the values
- use addComponentColumns to get components like CheckBox,TextField.
/
Grid grid = new Grid();
grid.setWidth(“1000px”);
grid.setItems(people);
grid.addColumn(Person::getName).setCaption(“Name”);
grid.addColumn(Person::getYear).setCaption(“Year of birth”).setId(“year”);
grid.addColumn(Person::getAmount).setCaption(“Amount”).setId(“amount”);
/* - creating HashMap with key as person class type and
- value as Chk_Txt class type where Chk_Txt class contains
- ChechBox and TextField.
/
HashMap<Person, Chk_Txt> hmChks = new HashMap<Person,Chk_Txt>();
/*
* addValueChangeListener fires event everytime, when value is changed
/
CheckBox1.addValueChangeListener(e ->{
//if CheckBox is not checked,every column CheckBoxes and TextFields are empty
if(!CheckBox1.getValue()){
for(Person p:people){
hmChks.get(p).getChk().setValue(CheckBox1.getValue());
hmChks.get(p).getTxt().setValue(“”);
}
}
/* - if amount is not entered in TextField, displays notification message
- if value entered in TextField, store value in amount_entered,
- compare it with amount of each column
/
else{
if(txtamount.getValue().isEmpty())
Notification.show(“Please enter amount”);
else
try{
int amount_entered = new Integer(txtamount.getValue());
System.out.println("bg: "+amount_entered);
for(Person p:people){
//check the CheckBoxes if amount matches
hmChks.get(p).getChk().setValue(CheckBox1.getValue());
/* - if amount entered by user is greater than amount in column
- set amount of that column, in TextField of grid
- else set user entered amount in TextField of grid
*/
if(amount_entered>=p.amount)
hmChks.get(p).getTxt().setValue(p.amount+“”);
else
hmChks.get(p).getTxt().setValue(amount_entered+“”);
amount_entered=amount_entered -(p.amount);
if(amount_entered<=0){
break;
}
}
}
catch(Exception ex){
Notification.show(“Invalid amount”);
}
}
}
);
/**
- add CheckBox to column using addComponentColumn
/
grid.addComponentColumn(Person → {
CheckBox chk = new CheckBox(“Chk 2”);
if(hmChks.containsKey(Person)){
hmChks.get(Person).setChk(chk);
}
else{
hmChks.put(Person, new Chk_Txt(chk));
}
Registration listenerRegistration = chk.addValueChangeListener(e->System.out.println("Person.amount: "+ Person.amount));
chk.addDetachListener(event → listenerRegistration.remove());
return chk;
}).setCaption(“ch2”).setId(“CH2”);
/* - add TextField to column using addComponentColumn
/
grid.addComponentColumn(Person → {
TextField text = new TextField();
if(hmChks.containsKey(Person)){
hmChks.get(Person).setTxt(text);
}
else{
hmChks.put(Person, new Chk_Txt(text));
}
return text;
}).setId(“2”);
// set CheckBox as Column header using column id Ch2.
grid.getHeaderRow(0).getCell(“CH2”).setComponent(CheckBox1);
/* - adding components to VerticalLayout
/
layout.addComponents(hlayout, grid);
setContent(layout);
}
@WebServlet(urlPatterns = "/", name = “MyUIServlet”, asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
//creating class Chk_Txt which has CheckBox and TextField
class Chk_Txt{
private CheckBox chk;
private TextField txt;
public Chk_Txt(CheckBox chk){
setChk(chk);
}
public Chk_Txt(TextField txt){
setTxt(txt);
}
public CheckBox getChk() {
return chk;
}
public void setChk(CheckBox chk) {
this.chk = chk;
}
public TextField getTxt() {
return txt;
}
public void setTxt(TextField txt) {
this.txt = txt;
}
}
}
[/code]Please, Help me out!