扩展AbstractTableModel并动态填充jTable [英] Extends AbstractTableModel and populate jTable dynamically

查看:110
本文介绍了扩展AbstractTableModel并动态填充jTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在研究客户端服务器体系结构,以便某些客户端可以在外部修改对象.

Basically, I'm working on a Client Server Architecture so Objects can be modified externally by some Clients.

我有银行:

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操作列表中添加一个操作.

Clients can add an Operation in the Bank operationList by calling addOperation().

问题是:JTable如何检测到并刷新显示?

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

因为客户端没有使用TableModel方法添加操作.他们无权参加此类课程. 最重要的是,我不知道在TableModel构造函数中给出Bank的整个operationList是否是一个好主意...

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

推荐答案

通过授予客户访问银行使用的内部列表的权限,您可以允许他们在银行的背后进行操作.有点像是真正的银行允许访问其内部数据库,而不是强迫所有客户通过在线银行应用程序.

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.

例如,接口实现的addOperation()方法不仅会将操作添加到操作列表中,还会触发一个表模型事件,以使表显示此添加的操作.

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.

这篇关于扩展AbstractTableModel并动态填充jTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆