Results 1 to 2 of 2
  1. #1
    thuytwee is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default How to dynamically refresh jtable in Netbeans ide?

    Thanks in advance for your help!

    I created a Jtable in Netbeans IDE 7.0.1 which pulls its data from a remote postgresql database. How can I get the jtable to dynamically update to display any changes in the database. Here's my code:

    package TablePackage;

    import java.awt.BorderLayout;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.AbstractTableModel;

    class TableTest
    {

    private static Connection con = null;
    private static String URL = "jdbc:postgresql://192.xxx.x.xxx:5432";
    private static String driver = "org.postgresql.Driver";
    private static String user = "xxxx";
    private static String pass = "xxxx";

    /**
    * Main application entry point
    * @param args
    * @throws SQLException
    */
    public static void main(String[] args) throws SQLException
    {

    // a Postgresql statement
    Statement stmt;
    // a Postgresql query
    String query;
    // the results from a Postgresql query
    ResultSet rs;

    // 2 dimension array to hold table contents
    // it holds temp values for now
    Object rowData[][] = {{"Row1-Column1", "Row1-Column2", "Row1-Column3", "Row1-Column4"}};
    // array to hold column names
    Object columnNames[] = {"IP", "Interface", "Status", "Speed"};

    // create a table model and table based on it
    DefaultTableModel mTableModel = new DefaultTableModel(rowData, columnNames);
    JTable table = new JTable(mTableModel);

    // try and connect to the database
    try {
    Class.forName(driver).newInstance();
    con = DriverManager.getConnection(URL, user, pass);
    } catch (Exception e) {
    System.err.println("Exception: " + e.getMessage());
    }

    // run the desired query
    query = "SELECT * FROM twee ORDER BY ip ASC;";
    //make a statement with the server
    stmt = con.createStatement();
    //execute the query and return the result
    rs = stmt.executeQuery(query);

    // create the gui
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(500, 300);
    frame.setVisible(true);

    // remove the temp row
    mTableModel.removeRow(0);

    // create a temporary object array to hold the result for each row
    Object[] rows;
    // for each row returned
    while (rs.next()) {
    // add the values to the temporary row
    rows = new Object[]{rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)};
    // add the temp row to the table
    mTableModel.addRow(rows);

    }

    }

    private TableTest()
    {
    }
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: How to dynamically refresh jtable in Netbeans ide?

    Whenever you query the data base you can create a new TableModel. Then to update the table you just do:

    Java Code:
    table.setModel(...);

Similar Threads

  1. Replies: 1
    Last Post: 10-02-2011, 09:41 PM
  2. Refresh JTable data
    By pink123 in forum AWT / Swing
    Replies: 1
    Last Post: 03-31-2011, 06:18 PM
  3. Could not able to refresh JTable.
    By pratim in forum AWT / Swing
    Replies: 3
    Last Post: 03-28-2011, 01:52 PM
  4. JTable refresh button.doClick()
    By javaevgny in forum AWT / Swing
    Replies: 6
    Last Post: 01-14-2011, 04:28 PM
  5. how to refresh data of the JTable
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-17-2008, 12:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •