You are here:

Java/Refreshing JTable


First of all i would like to thank you for all helps!
i have a test program that reads data from mysql and displays the data in JTable. I have two questions
1. Is the Jtable able to automatically check if the data has changed in the database without any user interaction like clicking any button or etc?

2. this is my program:

import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;

import java.awt.Font;
import java.util.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;

public class GetFruits implements TableModelListener{
  //Connection fruitsConnection;
  DefaultTableModel fruitsTableModel = new DefaultTableModel();
  JTable fruitsTable;
  //Getting data from Mysql database
  public JScrollPane fruitsTable() throws SQLException
     ConnectingToMysql fruitsConnection = new ConnectingToMysql();
     Connection myConnection = fruitsConnection.ConnectingToMysql();
     Statement st = myConnection.createStatement();
     ResultSet result = st.executeQuery("Select * from fruits");
     ResultSetMetaData metaData = result.getMetaData();
     int columnCount = metaData.getColumnCount();
     Font font = new Font("Courier", Font.PLAIN,16);   
     fruitsTable = new JTable(fruitsTableModel);
     fruitsTable.setPreferredScrollableViewportSize(new Dimension(550,310));
     JScrollPane fruitsScrollPane = new JScrollPane(fruitsTable);
     for(int i = 1; i <= columnCount; i++)
        Vector<String> row = new Vector<String>(columnCount);
        for(int i = 1; i <= columnCount; i++)
     return fruitsScrollPane;

  //creating a button to refresh data in JTable
  public void loadData()
     try {

     } catch (SQLException e) {
        // TODO Auto-generated catch block
  //applying the tableChanged method
  public void tableChanged(TableModelEvent e)
     int row = e.getFirstRow();   
     int column = e.getColumn();
     TableModel model = (TableModel)e.getSource();
     String columnName = model.getColumnName(column);
     Object data = model.getValueAt(row,column);
  //A panel to store the JTable that views data from database
  private JPanel fruitsTablePanel() throws SQLException
     JPanel fruitsTablePanel = new JPanel();
     fruitsTablePanel.setBorder(new TitledBorder("FRUITS TABLE"));
     return fruitsTablePanel;

  //A button to insert new record
  private JButton addButton()
     JButton addButton = new JButton("New Record");
     addButton.setPreferredSize(new Dimension(100,40));
     addButton.addActionListener(new ActionListener()
        public void actionPerformed(ActionEvent event)
         fruitAddForm addform = new fruitAddForm();
     return addButton;

  //A button to edit records
  private JButton editButton()
     JButton editButton = new JButton("Edit Record");
     editButton.setPreferredSize(new Dimension(100,40));
     return editButton;
  private JButton refreshButton()
     JButton deleteButton = new JButton("Refresh Record");
     deleteButton.setPreferredSize(new Dimension(100,40));
     deleteButton.addActionListener(new ActionListener()
        public void actionPerformed(ActionEvent e)
     return deleteButton;
  private JButton searchButton()
     JButton searchButton = new JButton("Search Record");
     searchButton.setPreferredSize(new Dimension(100,40));
     return searchButton;

  //A panel to store the add, delete, edit etc buttons
  private JPanel actionsPanel()
     JPanel actionsPanel = new JPanel();
     actionsPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
     actionsPanel.setPreferredSize(new Dimension(600,130));
     actionsPanel.setBorder(new TitledBorder("Actions"));
     return actionsPanel;
  //A control panel that stores other panels like actions panel
  private JPanel controlPanel()
     JPanel controlPanel = new JPanel();
     controlPanel.setLayout(new BorderLayout(1,2));
     controlPanel.add(actionsPanel(), BorderLayout.WEST);
     return controlPanel;

  //This panel houses all other panels in the gui
  private JPanel mainPanel() throws SQLException
     JPanel mainPanel = new JPanel();
     mainPanel.setLayout(new BorderLayout(1,2));
     mainPanel.setPreferredSize(new Dimension(600,485));
     mainPanel.add(fruitsTablePanel(), BorderLayout.NORTH);
     mainPanel.add(controlPanel(), BorderLayout.CENTER);
     return mainPanel;
  public void createWindowGUI()
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
         if ("Nimbus".equals(info.getName()))
        JFrame frame = new JFrame("Fruits");
     catch(Exception ex)

when clicking the refreshButton it refreshes the JTable but also adds extra empty rows. and also when clicking the addButton a form pops up letting the user to add more data. when the user clicks save button the add from closes but does not refresh the JTable!

again thanks

The best way to do this is to implement your own TableModel instead of using the DefaultTableModel. To make this easier, your model can extend AbstractTableModel which will take care of many of the details like the listeners. Since you define the model, when JTable initializes it will ask the model how many rows, columns and whatnot there are, which you can use simple DB queries to get. When you implement the setValueAt method you can perform a DB update (which JTable will follow up with a getValueAt to determine if the value has been actually changed and how so).

For changes from the DB, you will need to poll the DB to determine when a change is made (by someone else). You will need to use a Timer to do this (use the one in java.util, as you don't want to hang the application while the DB query is done).

Look at for details on what methods you want to implement, and the default behavior if you don't implement them.


All Answers

Answers by Expert:

Ask Experts


Artemus Harper


I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.


I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Washington State University

MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 All rights reserved.