JNoob
JNoob

Reputation: 3

Refreshing the java Jtable to show updated data

I am trying to refresh my Jtable shown in the UI whenever I query the mysql database. The idea was to show whatever new data updated in the UI JTable.

The UI class is below.

public class DBView {

private JFrame frame = new JFrame();    

private JScrollPane tableScrollPane = new JScrollPane();
private DefaultTableModel dbTable = new DefaultTableModel(); 


public void setDbTable(DefaultTableModel dbTable) {
    this.dbTable = dbTable;
    //this.dbTable.repaint();
    paintDBTable();

}

public DefaultTableModel getDbTable() {
    return dbTable;
}
public DBView() {
    initializeFrame();
    paintDBTable();

}

private void paintDBTable() {


    tableScrollPane.setBounds(20, 350, 400, 80);
    frame.getContentPane().add(tableScrollPane);
    JTable DBTable = new JTable(dbTable);
    tableScrollPane.add(DBTable);
    DBTable.setFillsViewportHeight(true);
    tableScrollPane.setViewportView(DBTable);

 }

/**
 * Initialize the contents of the frame.
 */
private void initializeFrame() {
    frame.setVisible(true);
    frame.setBounds(100, 100, 451, 525);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    frame.setTitle("MySQL Database");
}

From another Model class I am calling the setDbTable() method. I input a new Jtable object to the setDbTable() method with data read from the database input into the new Jtable object.

The issue is inside the setDbTable() method, I am using paintDBTable() method again.

I tried using dbTable.fireTableDataChanged() method to refresh the view, didnt work.

The way it is now, it is working. But using the setDbTable() method to refresh seems like a very inefficient way to do it.

Question is Do you see anyway I could use another method defined for use of refreshing Jtables?

P.S. I am very new to java and programming in general. Sorry if the code is messy and the question is unclear. I can give all the code if its helpful. I removed most of the methods and other classes in the original code to make the question clearer.

Upvotes: 0

Views: 903

Answers (2)

STaefi
STaefi

Reputation: 4377

It is not so confusing to refresh the JTable data and refreshing the UI after that, because:

Swing components implemented MVC and Observer in a very fantastic way. That means whenever you change the data in TableModel, the UI will be notified and repainted as you wanted.

So you should change you code in a way that you keep the JTable variable not the TableModel variable in your class. After that in setDbTable call the setModel method of the JTable, it means:

public class DBView {

    private JTable jtable = new JTable(); 

    public void setDbTable(DefaultTableModel dbTable) {
        this.jtable.setModel(dbTable);
        //this.dbTable.repaint(); 
        //paintDBTable();
    }
    .
    .
    .
}

Hope this would be helpful, Good Luck.

Upvotes: 0

mKorbel
mKorbel

Reputation: 109823

  1. tableScrollPane.add(DBTable);

  2. DefaultTableModel dbTable = new DefaultTableModel();

    • DefaultTableModel is model that hold value for presentations layer for the JTable
    • rename this local variable (that make the sence) to dbTableModel instead of dbTable

    • you have to create a JTables view, f.e. two basics options

      a) JTable myTable = new JTable(dbTableModel)

      b) myTable.setModel(dbTableModel)

  3. dbTable.fireTableDataChanged() is implemented in DefaultTableModel and correctly, not reason to call this method, nor outside of models definition (class, void, interface that returns XxxTableModel)

  4. more informations in linked Oracle tutorials, ... for working code examples in SSCCE / MCVE form too

  5. refresh data for JTable by

    • removing all rows in dbTableModel.setRowsCount(0);, then add a new row(s) to dbTableModel.addXxx

    • re_creating dbTableModel, note then must be added back to JTable e.g. myTable.setModel(dbTableModel)

Upvotes: 1

Related Questions