Search code examples
javaswingjtableswingworker

Update JTable in SwingWorker


I have a JTable which populates data from DB. I want to refresh data in JTable every 10 minutes (for testing 10 sec is enough). I tried do it with a Thread, but I found that it is not good idea, and I need to use SwingWorker

public class Monitor extends JFrame{

    JTable jtable = null;
    JTabbedPane jtp = null;
    JPanel jp1 = null;
    JPanel jp2 = null;
    JLabel label1 = null;
    JLabel label2 = null;

    public void setJTable(Vector data, Vector columnNames) {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jtable = new JTable(data, columnNames);
        JScrollPane scrollPane = new JScrollPane(jtable);
        jp1.add(scrollPane);
    }

    public void updateJTable(Vector data, Vector columnNames) {
        jtable = new JTable(data, columnNames);
    }

    public Monitor() {

        setTitle("Monitor System");

        //Panel with tabs for navigation
        jtp = new JTabbedPane();
        getContentPane().add(jtp);

        //tab1, info from dba_jobs
        jp1 = new JPanel();

        //tab2 info from QueueInfo
        jp2 = new JPanel();
        label1 = new JLabel();
        label1.setText("tab1");

        label2 = new JLabel();
        label2.setText("tab2");

        jp1.add(label1);
        jp2.add(label2);

        jtp.add("Tab1", jp1);
        jtp.add("Tab2", jp2);
    }

}

And my Demo class:

public class Demo {

    public static void main(String[] args) throws ClassNotFoundException, SQLException  {

        Statement stmt = null;
        Connection conn = null;
        ResultSet rs = null;
        Vector<String> columnNames = new Vector<String>();
        Vector<Vector> rowData = new Vector<Vector>();
        DBMonitor dbmonitor = new DBMonitor();
        Monitor monitor = new Monitor();

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        rowData = dbmonitor.getJobsData();
        columnNames = dbmonitor.getColumnNames();
        monitor.setJTable(rowData, columnNames);
        monitor.setSize((int) dim.getWidth(), (int) dim.getHeight());
        monitor.setVisible(true);

        boolean interrupt = true;

        while (interrupt) {
            try {

                rowData = dbmonitor.getJobsData();
                columnNames = dbmonitor.getColumnNames();
                monitor.updateJTable(rowData, columnNames);
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException ie) {
                    return;
                } 
                JOptionPane.showMessageDialog(null, "SLEEP!");

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
                System.err.println(e.getMessage());
            }
        }

    }
}

How I can do it with SwingWorker? I don't get a concept of that way.


Solution

  • In the doInBackground() method of the SwingWorker you have you while loop that:

    1. retrieves the data from the database
    2. creates your TableModel
    3. use the publish() method of the SwingWorker to pass the TableModel to the 'process()` method of your SwingWorker
    4. sleeps for 10 seconds

    Then in the process() method of the SwingWorker you:

    1. use the TableModel that was passed to the process() method to update your JTable.

    Read the section from the Swing tutorial on Concurrency for more information and a working example or search the forum for more SwingWorker examples.