Extends AbstractTableModel and populate jTable dynamically

假如想象 提交于 2019-12-25 09:40:08

问题


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().

The question is: How can the JTable detect that and refresh the display?

Because Clients are not using the TableModel methods for adding Operations. 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...


回答1:


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.

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.



来源:https://stackoverflow.com/questions/13683306/extends-abstracttablemodel-and-populate-jtable-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!