java用于多个表模型的ArrayList [英] java same ArrayList for multiple table model

查看:135
本文介绍了java用于多个表模型的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力避免使用多个JTable进行数据复制。基本上我有一个TableModel,它有一个数据的arraylist和一个string []标题。

I am struggling to avoid data duplication with multiple JTable. Basically I have a TableModel which has an arraylist of data and a string[] header.

到目前为止还没什么新东西。现在我有另一个TableModel具有相同的数据arraylist但是不同的string []标题。

So far nothing new. Now I have another TableModel which has the same arraylist of data BUT a different string[] header.

我无法让我的代码工作。我很感激如何跨多个表模型共享数据的arrayList。

I can't make my code to work. I would appreciate some idea on how to share the arrayList of data across multiple table model.

因此,当我更改数据时,所有模型都会更新,并且没有数据重复。我想避免复制arraylist
任何想法或我错误的设置?我确实尝试过监听器,但我认为这样我们仍然在表模型中有数据重复。

So when I change the data all models are updated and there isn't data duplication. I would like to avoid copy of arraylist any idea or am I wrong with this setup? I did try listener but I think going that way we still have data duplication across table model.

public class GymTableModel extends AbstractTableModel {
    private final ResourceBundle name = ResourceBundle.getBundle("labels/TableLabel", Locale.getDefault());
    private final String[] entete = {
        name.getString("LNAME"),
        name.getString("FNAME"),
        name.getString("CONTACT"),
        name.getString("TELFIX"),
        name.getString("TELGSM"),
        name.getString("EMAIL"),
        name.getString("BDATE"),
        name.getString("EDATE"),
        name.getString("LICENSE"),
        name.getString("AGE"),
        name.getString("ANCIEN")
    };

    @Override
    public Class<?> getColumnClass(int columnIndex){
        switch(columnIndex){
            case 6:
                return Date.class;
            case 7:
                return Date.class;
            case 9:
                return Integer.class;
            default:
                return Object.class;
        }
    }

    @Override
    public int getRowCount() {
        return gymList.size();
    }

    @Override
    public int getColumnCount() {
        return entete.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex){
            case 0:
                return gymList.get(rowIndex).getLastName();
            case 1:
                return gymList.get(rowIndex).getFirstName();
            case 2:
                return gymList.get(rowIndex).getContact();
            case 3:
                return gymList.get(rowIndex).getTelFix();
            case 4:
                return gymList.get(rowIndex).getTelGsm();
            case 5:
                return gymList.get(rowIndex).getEmail();
            case 6:
                return gymList.get(rowIndex).getDateBirth();
            case 7:
                return gymList.get(rowIndex).getDateClub();
            case 8:
                return gymList.get(rowIndex).getLicence();
            case 9:
                return gymList.get(rowIndex).getAge();
            case 10:
                return gymList.get(rowIndex).getAnciennete();
            default:
                return null; // should never happen;
        }
    }

    public String getColumnName(int columnIndex){
        return entete[columnIndex];
    }

}

现在我有另一张桌子了相同的数据,但只有3列(LNAME,FNAME和AGE),所以getColumnClass不相同。问题是,我可以与另一个表模型共享我的gymList数据(这是一个arrayList)吗?
谢谢你

Now I have another table which has same data but only 3 columns (LNAME, FNAME and AGE) so getColumnClass isn't same. Question is, can I share my gymList data (which is an arrayList) with another table model? Thankyou

推荐答案

使用Che的答案作为包含四列的主表的基础,那么你的代码应该像:

Using Che's answer as a basis for a main table containing four columns then your code should be something like:

AllTableModel model = new AllTableModel();
JTable mainTable = new JTable( model );

如果你想要另一个查看的模型然后你创建一个新视图而不是新模型。所以代码是:

If you want another View of the model then you create a new view not a new model. So the code would be:

JTable studentTable = new JTable( model );
studentTable.removeColumn( studentTable.getColumn("Subject") );
studentTable.removeColumn( studentTable.getColumn("Staff") );

现在学生表只包含名称和成绩列。

Now the Student table will only contain the "Name" and "Grade" columns.

这是MVC编程背后的基础。也就是说,您可以轻松更改模型查看。因此,您共享1个不共享3个独立模型数据的模型。

This is the basis behind MVC programming. That is you can easily change the View of the Model. So you are sharing 1 model not sharing the data of 3 separate models.

这篇关于java用于多个表模型的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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