JTable Not Updating Data

前端 未结 2 373
迷失自我
迷失自我 2021-01-22 12:59

For some reason, nothing changes about this JTable when this is called (this method updates the JTable after a user submits an SQL query).

Givens: The dataVector and col

相关标签:
2条回答
  • 2021-01-22 13:36

    It's a common beginner's fallacy to confuse objects with reference variables, but you need to understand that they are quite distinct. When you call this:

    table = new JTable(dataVector, columnNamesVector) {.....
    

    You are creating a new JTable object and having the table variable refer to it, but this has no effect on the JTable object that is displayed by the GUI, the one that the table variable was referring to previously. So you're changing the property of the reference variable, but leaving the original object unchanged.

    The solution: You should not be creating a new JTable but rather you should be creating a new TableModel and then place that TableModel into the existing and visualized JTable. You can change a table's model by calling setModel(newModel) on it.

    Edit: or as wolfcastle noted you could update the existing TableModel rather than create one anew.

    0 讨论(0)
  • 2021-01-22 13:51

    You need to tell JTable that the data in model was updated with firing appropriate event.

    See this tutorial

    0 讨论(0)
提交回复
热议问题