【发布时间】:2012-03-20 16:29:44
【问题描述】:
我有两个 JTable。一个是发票清单,另一个是发票中的产品清单。我创建了一个侦听器,以便通过单击鼠标从第一个 JTable 获取发票的 id 值,然后使用另一种方法为其他表生成新的表内容(方法工作正常)。但是,在更新表格后,我试图找到刷新它的方法,但没有成功......我只是在寻找一种在触发鼠标事件时刷新第二个表格的方法
以下是部分代码:
具有两个 JTable 的面板
public JPanel tabInvoices() {
final JPanel panel = new JPanel(new MigLayout("", "20 [grow, fill] 10 [grow, fill] 20", "20 [] 10 [] 20"));
final DefaultTableModel model1;
final JTable table1, table2;
String data[][] = {};
String col1[] = {"ID", "Date"};
String col2[] = {"ID", "Name", "Type", "Price", "Quantity"};
Object[][] selrowData = {};
/** Labels and buttons **/
JLabel labelInv = new JLabel("List of all invoices");
JLabel labelPro = new JLabel("List of all products in this invoice");
/** TABLE: Invoices **/
model1 = new DefaultTableModel(data, col1);
invoiceInfo = new DefaultTableModel(selrowData,col2);
/** Load the invoices from DB **/
List<Invoice> listInv = is.getAllInvoices();
for (int i = 0; i < listInv.size(); i++) {
model1.insertRow(i, new Object[] {
listInv.get(i).getID(),
listInv.get(i).getDate()
});
}
/** TABLE: Invoices **/
table1 = new JTable(model1){
public boolean isCellEditable(int r, int c) {
return false;
}
};
/** TABLE: Invoice Info **/
table2 = new JTable(invoiceInfo){
public boolean isCellEditable(int r, int c) {
return false;
}
};
/** Cell and Row selection listener **/
ListSelectionModel cellSelectionModel = table1.getSelectionModel();
cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent arg0) {
int id = (int)table1.getValueAt(row, 0); // Get ID from first table
invoiceInfo.getDataVector().removeAllElements(); // Remove everything from second table
invoiceInfo = getInvoiceInfo(id, invoiceInfo); // replace with new values
/**
* HERE'S WHERE I NEED TO REFRESH THE TABLE2. HOW?
*/
}
});
/** Load the products from DB belonging to this invoice **/
List<Product> listPro = is.getInvoiceInfo(row); // TODO Listener!
for (int i = 0; i < listPro.size(); i++) {
invoiceInfo.insertRow(i, new Object[] {
listPro.get(i).getID(),
listPro.get(i).getName(),
listPro.get(i).getType(),
listPro.get(i).getPrice(),
listPro.get(i).getQuantity()
});
}
/** Scroll Panes **/
JScrollPane scrollInv = new JScrollPane(table1);
JScrollPane scrollPro = new JScrollPane(table2);
/** Add everything to the panel **/
panel.add(labelInv);
panel.add(labelPro, "wrap");
panel.add(scrollInv);
panel.add(scrollPro);
return panel;
}
每次单击左表中的任何单元格时用于重新创建第二个表的方法:
public DefaultTableModel getInvoiceInfo(int id, DefaultTableModel model) {
String data1[][] = {};
String col1[] = {"ID", "Name", "Type", "Price", "Quantity"};
DefaultTableModel info = new DefaultTableModel(data1,col1);
/** Load products from DB **/
List<Product> products = is.getInvoiceInfo(id);
for (int i = 0; i < products.size(); i++) {
info.insertRow(i, new Object[] {
products.get(i).getID(),
products.get(i).getName(),
products.get(i).getType(),
products.get(i).getPrice(),
products.get(i).getQuantity()
});
}
return info;
}
【问题讨论】:
-
欢迎来到 GUI 世界,不要让摆动的复杂性让你失望:)
标签: java swing jtable actionlistener repaint