简体   繁体   English

如何更新JTable视图

[英]How to Update a JTable View

I have a small app project I'm working on that involves JDBC and a MySQL database. 我有一个正在处理的小型应用程序项目,涉及JDBC和MySQL数据库。 I am trying to adhere to MVC architecture while making it so, the main form for interaction is the controller. 我试图同时坚持MVC架构,交互的主要形式是控制器。 When a user searches the database based off a Find button being pressed it creates a new JTable view to display the search results: 当用户基于按下的“查找”按钮搜索数据库时,它将创建一个新的JTable视图以显示搜索结果:

model = DbUtils.resultSetToTableModel( results );
view = new Tunes_Peddler_View( model );

If I press the Find button again though, a new instance of the JTable is created whereas I want the existing window to update with the results from the new query. 但是,如果再次按“查找”按钮,则会创建JTable的新实例,而我希望现有窗口用新查询的结果进行更新。

Here is the code for the DbUtils class that creates a table model from a result object: 这是DbUtils类的代码,该类从结果对象创建表模型:

public static TableModel resultSetToTableModel(ResultSet rs)
{
    try 
    {
        //get the the metadata for number of columns and column names
        ResultSetMetaData metaData = rs.getMetaData();
        int numberOfColumns = metaData.getColumnCount();
        Vector<String> columnNames = new Vector<String>();

        // Get the column names and store in vector
        for (int column = 0; column < numberOfColumns; column++)
        {
            columnNames.addElement(metaData.getColumnLabel(column + 1));
            //NOTE: need to do the (+1) because metaData columns indexing starts at 1
            //but JTable column numbering starts at 0. 
        }

        // Get all rows data and store in a Vector.
        Vector<Object> rows = new Vector<Object>();

        rs.first();
        do 
        {
            Vector<Object> newRow = new Vector<Object>();

            for (int i = 1; i <= numberOfColumns; i++) {
                 newRow.addElement(rs.getObject(i));
             } 

            rows.addElement(newRow);
         }while (rs.next());

        return new DefaultTableModel(rows, columnNames);
     } catch (Exception e) 
     {
         System.out.println("Exception in DbUtils method resultSetToTableModel()...");
         e.printStackTrace();

         return null;
     }//end catch
 }//end method

And the code for creating the actual view itself: 以及用于创建实际视图本身的代码:

public Tunes_Peddler_View(TableModel model)
{
    // Call super.
    super("Returned Records");

    // Boilerplate
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(1000, 200);
    this.setLocationRelativeTo( null );

    // Create the JTable and pass it the TableModel as an argument
    JTable table = new JTable( model );
    table.setModel(model );

    // Create a JScrollPane to hold the JTable
    JScrollPane scrollPane = new JScrollPane( table );
    this.add( scrollPane );

    // Last line
    this.setVisible(true);
}

In order to make the JTable view just update instead of creating a new view should I just add a listener to it or how should I go about it? 为了使JTable视图只是更新而不是创建新视图,我应该向它添加一个侦听器还是应该怎么做?

Thanks. 谢谢。

Don't create a new instance of the view, instead, maintain a single reference to the view within the controller, when you get the new model, pass this to the instance of the view and allow the view to set model of the table 不要创建视图的新实例,而是在控制器中维护对视图的单个引用,当您获得新模型时,将其传递给视图的实例,并允许视图设置表的模型

This is at the core of how MVC is suppose to work 这是MVC如何工作的核心

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

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