简体   繁体   中英

How to Update a JTable View

I have a small app project I'm working on that involves JDBC and a MySQL database. I am trying to adhere to MVC architecture while making it so, the main form for interaction is the controller. When a user searches the database based off a Find button being pressed it creates a new JTable view to display the search results:

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.

Here is the code for the DbUtils class that creates a table model from a result object:

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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