应该在哪里定义我的可选DefaultTableModel方法? [英] Where should definition my optional DefaultTableModel methods?

查看:85
本文介绍了应该在哪里定义我的可选DefaultTableModel方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这3类将数据从数据库显示到JTable.

I use this 3 class for show data from database into JTable.

public class TableContent {

private final Vector<String> headers;
private final Vector<Vector<String>> content;

public TableContent(final Vector<String> headers, final Vector<Vector<String>> content) {
this.headers = headers;
this.content = content;
  }

public Vector<String> headers() {
return headers;
}

public Vector<Vector<String>> content() {
return content;
}

并且:

public class TableData {

public TableContent getData() {
Vector<String> headers = new Vector<String>();
Vector<Vector<String>> content = new Vector<Vector<String>>();

try {
    Connection conn = DriverManager.getConnection("");
    Statement statement = conn.createStatement();
    ResultSet rs = statement.executeQuery("Select * from table");

    headers = buildHeaders(rs);
    content = buildContent(rs);

} catch (SQLException e) {
    e.printStackTrace();
}

return new TableContent(headers, content);
 }

private Vector<String> buildHeaders(final ResultSet rs) throws SQLException {
Vector<String> headers = new Vector<String>();

int col = rs.getMetaData().getColumnCount();
for (int i = 1; i <= col; i++) {
    headers.add(rs.getMetaData().getColumnName(i));
    }
return headers;
}

private Vector<Vector<String>> buildContent(final ResultSet rs) throws SQLException {
Vector<Vector<String>> content = new Vector<Vector<String>>();

while (rs.next()) {
    int col = rs.getMetaData().getColumnCount();
    Vector<String> newRow = new Vector<String>(col);

    for (int i = 1; i <= col; i++) {
        newRow.add(rs.getString(i));
    }
    content.add(newRow);
 }
return content;
  }
 }
}

并且:

public class TableGUI extends JFrame {
// Create GUI and Show table
 }

以前,我将我的方法添加到扩展的DefaultTableModel类中,现在该如何定义该方法以及在哪里进行呢?

Formerly i add my methods to my extended DefaultTableModel Class, Now how can i define this methods and where should do it?

更新:

对于e.x,我测试了此方法:

for e.x I test this method:

public class TableContent {
String dbUrl = "...";

private final Vector<String> headers;
private final Vector<Vector<String>> content;

public TableContent(final Vector<String> hdr, final Vector<Vector<String>> cnt) {
    headers = hdr;
    content = cnt;
}

public Vector<String> headers() {
    return headers;
}

public Vector<Vector<String>> content() {
    return content;
}
public void removeRow(int modelRow, Object rowID) {
    String removeQuery = "delete from table where id=?";
    Connection conn;
    PreparedStatement ps = null;
    try {
        conn = DriverManager.getConnection(...);
        ps = conn.prepareStatement(removeQuery);
        Object idValue = rowID;
        ps.setObject(1, idValue);
        if (ps.executeUpdate() == 1) {
            content.remove(modelRow);
         fireTableRowsDeleted(modelRow, modelRow);   // Error
        }
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
}
}

现在在这里,问题是fireTableRowsDeleted(...);

问题意味着IDE会说cannot resolve method fireTableRowsDeleted(int,int)`

Problem means that IDE say that cannot resolve methodfireTableRowsDeleted(int,int)`

推荐答案

关于此主题的第十个问题是什么?为什么您发布的用于创建TableModel的代码与我在第9次发布中建议的代码看起来不一样?在该代码示例中,我向您展示了如何从方法返回TableModel.相反,您的代码创建了两个Vector,并试图维护对这些Vector的引用.那不是做到这一点的方法. DefaultTableModel包含数据,您不需要数据的单独副本.您可以使用getValueAt(..)方法访问模型中的数据.

What is this the 10th question on this topic? Why does your posted code to create a TableModel not look like the code I suggested in your 9th posting? In that code example I showed you how to return a TableModel from a method. Instead your code is creating two Vectors and trying to maintain a reference to those Vectors. That is NOT the way to do it. The DefaultTableModel contains the data, you don't need a separate copy of the data. You can access the data in the model by using the getValueAt(..) method.

我一直建议您有两种选择:

As I have been suggesting you have two options:

  1. 使用方法更新数据库和TableModel创建一个帮助器类
  2. 扩展DefaultTableModel以添加这些方法.

对于帮助程序类,您将定义一个方法,例如:

For a helper class you would define a method like:

public void removeRow(DefaultTableModel model, int modelRow)
{
    Object rowID = model.getValueAt(modelRow, theColumn);

    // your code to delete row from database

    if (row delected successfully)
        model.removeRow(modelRow);
}

您不需要rowID作为参数,因为您可以使用getValueAt()方法从模型中获取数据.这个想法是使用DefaultTableModel的方法来管理数据,而不是重新发明轮子.您要做的只是添加代码以更新数据库.

You don't need the rowID as a parameter because you can get the data from the model by using the getValueAt() method. The idea is to use the methods of the DefaultTableModel to manage the data and not reinvent the wheel. All you are doing is adding code to update the database.

如果要通过扩展DefaultTableModel来创建CustomTableModel,则removeRow()方法不需要DefaultTableModel作为参数,可以调用DefaultTableModel的super.removeRow()方法.

If you want to create a CustomTableModel by extending the DefaultTableModel, then your removeRow() method does not need the DefaultTableModel as a parameter and you can invoke super.removeRow() method of the DefaultTableModel.

这篇关于应该在哪里定义我的可选DefaultTableModel方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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