I want to create JSF table which has select all button and check box in every row. I found this source code example. Following this JSF table tutorial I found this JSF table with select row example. I tried to modify the code but I get error in Netbeans which I don’t know how to fix:
import java.io.Serializable;
import java.math.BigDecimal;
import javax.enterprise.context.SessionScoped;
// or import javax.faces.bean.SessionScoped;
import javax.inject.Named;
/* include SQL Packages */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
// or import javax.faces.bean.ManagedBean;
import org.glassfish.osgicdi.OSGiService;
@Named("SessionsController")
@SessionScoped
public class Sessions implements Serializable {
public Sessions() {
}
private Map<Long, Boolean> selectedIds = new HashMap<Long, Boolean>();
private List<MyData> selectedDataList;
/* Call the Oracle JDBC Connection driver */
@Resource(name = "jdbc/Oracle")
private DataSource ds;
private static final ArrayList<SessionArray> sessionList =
new ArrayList<SessionArray>(Arrays.asList(
new SessionArray("A0001", "Intel CPU",
new BigDecimal("700.00"), 1),
new SessionArray("A0002", "Harddisk 10TB",
new BigDecimal("500.00"), 2),
new SessionArray("A0003", "Dell Laptop",
new BigDecimal("11600.00"), 8),
new SessionArray("A0004", "Samsung LCD",
new BigDecimal("5200.00"), 3),
new SessionArray("A0005", "A4Tech Mouse",
new BigDecimal("100.00"), 10)));
public ArrayList<SessionArray> getSessionList() {
return sessionList;
}
public String deleteAction(SessionArray session) {
sessionList.remove(session);
return null;
}
public static class SessionArray {
String orderNo;
String productName;
BigDecimal price;
int qty;
public SessionArray(String orderNo, String productName,
BigDecimal price, int qty) {
this.orderNo = orderNo;
this.productName = productName;
this.price = price;
this.qty = qty;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
}
//select all button code
public String getSelectedItems() {
// Get selected items.
selectedDataList = new ArrayList<MyData>();
for (MyData dataItem : dataList) {
if (selectedIds.get(dataItem.getId()).booleanValue()) {
selectedDataList.add(dataItem);
selectedIds.remove(dataItem.getId()); // Reset.
}
}
// Do your thing with the MyData items in List selectedDataList.
return "selected"; // Navigation case.
}
public Map<Long, Boolean> getSelectedIds() {
return selectedIds;
}
public List<MyData> getSelectedDataList() {
return selectedDataList;
}
}
Netbeans gave me an error whrn I want to use this code private List<MyData> selectedDataList;. MyData Class is not found. How I can fix this error?
Best wishes
Do you have the class
MyDatain your project/workspace…If not implement it… if it is present, import it
In case you took the code from some example, double check that you haven’t forgot anything…