Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
To get the selected row value on click of edit button in a particular row
Hi,
Please help me on finding the value of row of table when i click on edit button in a row when the row is not selected as i want to update the value of row in a new window .
please find the below code and let me know where i am getting it wrong .on click of edit button i need to get the value of row to be populated in edit window
NOTE: now i am able to get the selected row that is table.selectable (true) but when i click on edit button in any row the value is coming for selectable row even though edit button clicked is not for that row
import java.util.Date;
import java.util.Iterator;
import java.util.List;
public class UserComponent extends CustomeComponent {
@AutoGenerated
private AbsoluteLayout mainLayout;
@AutoGenerated
private Button addNewUSer;
@AutoGenerated
private Table userTable;
public UserComponent() {
buildMainLayout();
setCompositionRoot(mainLayout);
// TODO add user code here
}
@AutoGenerated
private AbsoluteLayout buildMainLayout() {
// common part: create layout
mainLayout = new AbsoluteLayout();
mainLayout.setImmediate(false);
mainLayout.setWidth("100%");
mainLayout.setHeight("100%");
mainLayout.setMargin(false);
// top-level component properties
setWidth("100.0%");
setHeight("100.0%");
// table_1
userTable = new Table();
userTable.setImmediate(false);
userTable.setWidth("100.0%");
userTable.setHeight("400px");
userTable.setSelectable(true);
mainLayout
.addComponent(userTable, "top:60.0px;right:76.0px;left:20.0px;");
userTable.addContainerProperty("Edit",Button.class,null);
userTable.addContainerProperty("Delete",Button.class,null);
userTable.addContainerProperty("Full Name",String.class,null);
userTable.addContainerProperty("Role",String.class,null);
userTable.addContainerProperty("EmailId",String.class,null);
userTable.addContainerProperty("Status",Boolean.class,null);
userTable.addContainerProperty("Created Date",Date.class,null);
userTable.addContainerProperty("Modified Date",Date.class,null);
userTable.addListener(new MyListener());
Button editButton=new Button("Edit");
// editButton.addListener(new DetailInfoListener());
Button deleteButton=new Button();
List<Users> usrList=getUserService().getUserData();
int i=0;
for (Iterator iterator = usrList.iterator(); iterator.hasNext();) {
Users users = (Users) iterator.next();
String firstName=users.getFirstname();
String lastName=users.getLastname();
String fullName=firstName+lastName;
userTable.addItem(new Object[] {
new Button("Edit",
new Button.ClickListener() {
/**
*
*/
private static final long serialVersionUID = 8034233094433430666L;
// inline click-listener
public void buttonClick(ClickEvent event) {
Table comp=(Table)event.getButton().getParent();
userTable.addListener(new MyListener());
Object rowId = userTable.getValue(); // get the selected rows id
if(rowId==null)
{
mainWindow.showNotification("please select one row");
}else{
String Role = (String)userTable.getContainerProperty(rowId,"Role").getValue();
}
}
}),new Button("Delete",
new Button.ClickListener() {
/**
*
*/
private static final long serialVersionUID = 8034233094433430666L;
// inline click-listener
public void buttonClick(ClickEvent event) {
System.out.println("I am in Button");
}
}),fullName,users.getRole(),users.getEmailid(),users.isStatus(),
users.getCreatedDate(),users.getModifiedDate()}, new Integer(i));
i++;
}
userTable.setColumnIcon("Edit", new ThemeResource("icons/16/refresh.png"));
// addNewUSer
addNewUSer = new Button();
addNewUSer.setCaption("Add New User");
addNewUSer.setImmediate(true);
addNewUSer.setWidth("-1px");
addNewUSer.setHeight("-1px");
mainLayout.addComponent(addNewUSer, "top:20.0px;left:680.0px;");
addNewUSer.addListener(new DetailInfoListener());
return mainLayout;
}
private class DetailInfoListener implements Button.ClickListener {
/**
*
*/
private static final long serialVersionUID = 2654154819399687691L;
@Override
public void buttonClick(ClickEvent event) {
}
}
private class MyListener implements Property.ValueChangeListener,Button.ClickListener {
/**
*
*/
private static final long serialVersionUID = 2654154819399687691L;
@Override
public void valueChange(ValueChangeEvent event) {
Object rowId = userTable.getValue(); // get the selected rows id
String email = (String)userTable.getContainerProperty(rowId,"EmailId").getValue();
System.out.println(email);
}
@Override
public void buttonClick(ClickEvent event) {
Object rowId = userTable.getValue(); // get the selected rows id
String email = (String)userTable.getContainerProperty(rowId,"EmailId").getValue();
System.out.println(email);
}
}
}
The code in my opinion would require quite a lot of refactoring, so here's a simple quick-and-dirty solution.
Button editButton = new Button("Edit",
new Button.ClickListener() {
// inline click-listener
public void buttonClick(ClickEvent event) {
Object rowId = event.getButton().getData();
String Role = (String)userTable.getContainerProperty(rowId,"Role").getValue();
}
}
});
editButton.setData(id);
userTable.addItem(new Object[] { editButton, // and the others
Johannes Häyry: The code in my opinion would require quite a lot of refactoring, so here's a simple quick-and-dirty solution.
Button editButton = new Button("Edit", new Button.ClickListener() { // inline click-listener public void buttonClick(ClickEvent event) { Object rowId = event.getButton().getData(); String Role = (String)userTable.getContainerProperty(rowId,"Role").getValue(); } } }); editButton.setData(id); userTable.addItem(new Object { editButton, // and the others
here In editButton.setData(id) how dynamically set the value of Id