简体   繁体   English

AbstractTableModel实现

[英]AbstractTableModel Implementation

I'm trying to view a table in a Jtable when a button is actioned.I hold the table in a two dimensional array and i implemented TableModel class but did not worked.I don't get an error but table doesn't appear.What am i missing? 我试图在操作按钮时查看Jtable中的表。我将表保存在二维数组中,并且实现了TableModel类,但没有用。我没有收到错误,但表未出现。我想念什么?

Here is my code : 这是我的代码:

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

class MyTableModel extends AbstractTableModel {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private ArrayList<ArrayList<Object>> list = new ArrayList<ArrayList<Object>>();
ResultSet rs;

@Override
public int getColumnCount() {
    try {
        ResultSetMetaData rsmd = rs.getMetaData();
        return rsmd.getColumnCount();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        return -1;
    }
}

@Override
public int getRowCount() {
    try {
        int rowCount = 0;
        while (rs.next())
            rowCount++;
        return rowCount;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return -1;
    }
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {

    return list.get(columnIndex).get(rowIndex);
}

public void add(ResultSet rs) throws SQLException {
    this.rs = rs;
    list = createListData(rs);
    this.fireTableDataChanged();
}

private ArrayList<ArrayList<Object>> createListData(ResultSet rs)
        throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int column = rsmd.getColumnCount();
    ArrayList<ArrayList<Object>> myList = new ArrayList<ArrayList<Object>>();
    ArrayList<Object> temp = new ArrayList<Object>();

    while (rs.next()) {
        temp = new ArrayList<Object>();
        for (int i = 1; i <= column; i++) {
            temp.add(rs.getString(i));
        }
        myList.add(temp);
    }
    System.out.println(myList.get(1).get(2));
    return list;
}
}

And here is action method : 这是动作方法:

private void Adress_onClick(JButton button) {
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {


            //model.addRegister("ADRESS_ID", "CITY", "TOWN", "DISTRICT");
            DBOperations dbOp = new DBOperations();
            try {
                Connection conn = dbOp.connect();
                ResultSet rs = dbOp.runQuery(conn, "SELECT * FROM ADRESS ORDER BY ADRESS_ID");
                MyTableModel model = new MyTableModel();
                model.add(rs);
                table.setModel(model);

            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }
    });

}

Your code assumes that the JTable will call some of your methods in a specific order. 您的代码假定JTable将按特定顺序调用某些方法。 You should not make such assumptions. 您不应该做出这样的假设。

For example, the method getRowCount() assumes that the resultset is before the first row, and goes to the last row to count the rows. 例如,方法getRowCount()假定结果集在第一行之前,然后转到最后一行以对行进行计数。 If it's called a second time, or if another method doing the same thing is called before, getRowCount() will start returning 0. 如果是第二次调用,或者之前调用了另一个执行相同操作的方法,则getRowCount()将开始返回0。

Separate your database access code from your GUI code. 将数据库访问代码与GUI代码分开。 When you want to display the data from a database query, execute the query in a method of a separate class, and make this method return a list of objects. 当您要显示数据库查询中的数据时,请在单独的类的方法中执行查询,并使该方法返回对象列表。 Then build an AbstractTableModel implementation using this list of objects. 然后使用此对象列表构建AbstractTableModel实现。 The table model shouldn't deal with result sets. 表模型不应处理结果集。

Your code is a mixture of using the result set (in getRowCount() and getColumnCount() for example), and using a list built from this result set (in getValueAt() , for example). 您的代码混合了以下两种情况:使用结果集(例如,在getRowCount()getColumnCount()中),以及使用根据该结果集构建的列表getValueAt()例如,在getValueAt() )。 Build the list before creating the model, then use this list only in your table model implementation. 在创建模型之前先构建列表,然后仅在表模型实现中使用此列表。 And fix the getValueAt() method: it should be 并修复getValueAt()方法:应该

return list.get(rowIndex).get(columnIndex);

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

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