简体   繁体   English

扩展AbstractTableModel并动态填充jTable

[英]Extends AbstractTableModel and populate jTable dynamically

Basically, I'm working on a Client Server Architecture so Objects can be modified externally by some Clients. 基本上,我正在研究客户端服务器体系结构,以便某些客户端可以在外部修改对象。

I have a bank : 我有银行:

public class Bank{
    private List<BankingOperation> operationList = new ArrayList<BankingOperation>();

    public void addOperation(BankingOperation op) {
        this.operationList.add(op);
//...
}

And my Server : 和我的服务器:

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;
    }//...
}

Clients can add an Operation in the Bank operationList by calling addOperation(). 客户可以通过调用addOperation()在Bank operationList中添加一个Operation。

The question is: How can the JTable detect that and refresh the display? 问题是:JTable如何检测到并刷新显示?

Because Clients are not using the TableModel methods for adding Operations. 因为客户端没有使用TableModel方法添加操作。 They have no access to this class. 他们无权访问此课程。 On top of that, I don't know if giving the whole operationList of the Bank in the TableModel constructor is a good idea... 最重要的是,我不知道在TableModel构造函数中给出Bank的整个operationList是否是个好主意...

By giving the clients access to the internal list used by the bank, you allow them to do things behind the bank of the bank. 通过授予客户访问银行使用的内部清单的权限,您就可以允许他们在银行的背后进行操作。 A bit like if a real bank gave access to its internal database, instead of forcing all the clients to go through the online bank application. 有点像是真正的银行允许访问其内部数据库,而不是强迫所有客户通过在线银行应用程序。

You should give the clients a reference to an interface which allows them to perform their operations. 您应该给客户端一个接口的参考,该接口允许他们执行操作。 The implementation of this interface would control that every operation they do is allowed, and does everything necessary. 此接口的实现将控制允许他们执行的每项操作,并执行所有必要的操作。

For example, the addOperation() method of the interface implementation would not only add the operation to the list of operations, but also fire a table model event in order for the table to display this added operation. 例如,接口实现的addOperation()方法不仅会将操作添加到操作列表中,还会触发一个表模型事件,以便表显示此添加的操作。

This could be done directly, if the bank encapsulates the table model, or indirectly, by having the bank fire a custom "operation added" event. 如果银行封装了表模型,则可以直接执行此操作,也可以通过让银行触发自定义的“添加操作”事件来间接执行。 The table model would listen to those events, and fire its own table model event in order for the table to be updated. 表模型将侦听这些事件,并触发其自己的表模型事件以更新表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM