DBOperations objDB;
ArrayList alstStock;
private TableRowSorter sorter;
public void viewDailyStock() {
initComponents();
setSize(Toolkit.getDefaultToolkit().getScreenSize());
objDB = new DBOperations();
alstStock = objDB.getStockList();
generateTable();
txtSearch.getDocument().addDocumentListener(
new DocumentListener() {
@Override
public void changedUpdate(DocumentEvent e) {
newFilter();
}
@Override
public void insertUpdate(DocumentEvent e) {
newFilter();
}
@Override
public void removeUpdate(DocumentEvent e) {
newFilter();
}
});
setVisible(true);
}
void generateTable() {
Object data[][] = new Object[alstStock.size()][10];
String date = null;
for (int i = 0; i < alstStock.size(); i++) {
StockBean objBean = (StockBean) alstStock.get(i);
data[i][0] = i + 1;
data[i][1] = objBean.getBrand();
data[i][2] = objBean.getProductCode();
data[i][3] = objBean.getProductName();
data[i][8] = objBean.getItemInBox();
SimpleDateFormat df = new SimpleDateFormat("EEEE, MMM d,yyyy");
date = df.format(objBean.getDate());
data[i][4] = date;
data[i][5] = objBean.getUser();
data[i][6] = objBean.getLotNumber();
data[i][7] = objBean.getQtyInItem();
data[i][9] = objBean.getQtyInBox();
}
String header[] = {"S.No.", "Brand", "Code", "Name", "Date", "Added By", "Lot No.", "Qty In Item", "Item", "Qty In Boxes"};
tblStock = new JTable(data,header);
sorter=new TableRowSorter(tblStock.getModel());
tblStock.setRowSorter(sorter);
jScrollPane1.setViewportView(tblStock);
setWidth();
tblStock.setAutoCreateRowSorter(true);
}
private void newFilter() {
RowFilter rf = null;
try {
rf = RowFilter.regexFilter(txtSearch.getText(), 3);
} catch (java.util.regex.PatternSyntaxException e) {
System.out.println("Exception "+e);
}
sorter.setRowFilter(rf);
}
DBOperations 是一个用于从数据库表中获取数据的类。StockBean 是一个具有getter 和setter 方法的对象类。
这是我使用文本字段对 jtable 的第 4 列实施行过滤器的代码,但它不起作用。请帮我...