简体   繁体   English

从DefaultTableModel到MVC架构中的JTable获取数据

[英]Getting Data From DefaultTableModel to a JTable In The MVC Architecture

I'm having a problem with separating The Table Model(in the Model) from my JTable (in the view), My model contains the query needed to get the data from the database with a buildTableModel() method: 我在从JTable(在视图中)中分离Table Model(模型中的)时遇到问题,我的模型包含使用buildTableModel()方法从数据库中获取数据所需的查询:

Model.java 模型

public static DefaultTableModel buildTableModel(ResultSet rs)
            throws SQLException {

        ResultSetMetaData metaData = rs.getMetaData();

        // names of columns
        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();
        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
        }

        // data of the table
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (rs.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }

        return new DefaultTableModel(data, columnNames);

    }

    //queries to execute for Jtable model listProduit
    public ResultSet productList()throws SQLException{
        ResultSet rs = stmt.executeQuery("SELECT * from Lot");

        return rs;
    }

View.java View.java

public class View extends JPanel{


    public  DefaultTableModel dt;

    public  JTable productTable;

    private JScrollPane scrolPane;

    public ListProduit(){


        productTable=new JTable(dt);

        this.setLayout(new GroupLayout(this));
        this.setBackground(Color.decode("#CFDBC5"));

        productTable.setPreferredScrollableViewportSize(new Dimension(500,50));
        productTable.setFillsViewportHeight(true);

        scrolPane= new JScrollPane(productTable);
        this.add(scrolPane);

        this.setVisible(true);


    } 

//setter for the table model
public void setDt(DefaultTableModel dt) {
        this.dt = dt;
        this.dt.fireTableDataChanged();
    }

And this is the part where I'm getting the problem: 这是我遇到问题的部分:

Controller.java Controller.java

            try{   //lines of the problem:
                    ResultSet rs= model.productList();
                    DefaultTableModel dtm = Model.buildTableModel(rs);
                    View.setDt(dtm);
                    // Stuff to handle showing the view
                    showFourthCard();
                    panelList.add(4);
                }catch(SQLException ex){
                    ex.printStackTrace();
                }

Apparentlly the JTable can not change the DefaultTbaleModel object that it gets in the first time so when I'm executing I always get a blank JTable, so in short I cannot set a new DefaultTableModel object in my Controller.java . 显然, JTable无法更改它第一次获得的DefaultTbaleModel对象,因此在执行时,我总是得到一个空白的JTable,因此总之,我无法在Controller.java设置新的DefaultTableModel对象。

Note: This works just fine when I'm not using MVC (because I don't have to set the Table Model), so my problem here is mainly with the separation of the JTable from the Table Model. 注意:当我不使用MVC时,这工作得很好(因为我不必设置表模型),所以这里的问题主要是JTable与表模型的分离。

Apparentlly the JTable can not change the DefaultTbaleModel object that it gets in the first time so when I'm executing I always get a blank JTable, so in short I cannot set a new DefaultTableModel object in my Controller.java. 显然,JTable无法更改它第一次获得的DefaultTbaleModel对象,因此在执行时,我总是得到一个空白的JTable,因此总之,我无法在Controller.java中设置新的DefaultTableModel对象。

public void setDt(DefaultTableModel dt) {
    this.dt = dt;
    this.dt.fireTableDataChanged();
} 

The code above only changes the field, it does not change the TableModel of the JTable instance (eg productTable , which is initially set in the constructor). 上面的代码仅更改字段,而不更改JTable实例的TableModel(例如, productTable ,它最初在构造函数中设置)。 Make a call to the JTable instance itself to actually change the model backing the JTable eg productTable.setModel(dt); 调用JTable实例本身以实际更改支持JTable的模型,例如productTable.setModel(dt);

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

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