基本上,我正在研究客户端服务器架构,因此某些客户端可以在外部修改对象。
我有一家银行:
public class Bank{
private List<BankingOperation> operationList = new ArrayList<BankingOperation>();
public void addOperation(BankingOperation op) {
this.operationList.add(op);
//...
}
我的服务器:
public class ServerBank extends JFrame {
private Bank bank;
private JTable table;
private OperationTableModel model;
public ServerBank() {
this.bank = new Bank();
this.model= new OperationTableModel(this.bank.getOperationList());
table = new JTable(model);
getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
pack();
}
public static void main (String args[]) throws Exception {
ServerBank frame=new ServerBank();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,700);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class OperationTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private List<BankingOperation> operationList;
public String[] colNames = { "Date", "Login", "Customer","Account", "Operation", "Amount", "Final Balance" };
public Class<?>[] colTypes = { String.class, String.class, String.class, String.class, String.class, Integer.class,
Integer.class };
public OperationTableModel(List<BankingOperation> operationList) {
super();
this.operationList = operationList;
}//...
}
客户可以通过调用 addOperation() 在 Bank operationList 中添加一个 Operation。
问题是:JTable 如何检测并刷新显示?
因为客户端没有使用 TableModel 方法来添加操作。他们无权访问此类。最重要的是,我不知道在 TableModel 构造函数中给出银行的整个 operationList 是否是一个好主意......