Thank you in advance for your help!

I am designing a GUI in Netbeans IDE 7.0.1 by dragging and dropping controls such as labels, text boxes and a table. The table's data is generated by connecting to a remote database, runs a thread that periodically queries the database, and displays any changes that might occur. I am customizing the code for the jtable by right clicking the table and Customize Code.

The issue I'm having is how to set the table's tablemodel table.setModel(mTableModel); in the customized code area for the jtable so that it displays the current data. Does anyone know how I can accomplish this?

Here is my code for the program:

package Switch;

import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Connection;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;

public class SwitchInterface extends javax.swing.JFrame {

//private Vector<Vector<String>> data; //used for data from database
//private Vector<String> header; //used to store data header

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

/** Creates new form SwitchInterface */
public SwitchInterface() throws Exception{

// 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"};

final DefaultTableModel mTableModel = new DefaultTableModel(rowData, columnNames);
final JTable table = new JTable(mTableModel);

// run the desired query
final String query = "SELECT * FROM xxx ORDER BY ip ASC;";

// try and connect to the database
try {
Class.forName(driver).newInstance();
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}

Runnable task = new Runnable() {

public void run() {

ResultSet rs = null;
while(true) {

try {

System.out.println("Querying DB");

con = DriverManager.getConnection(URL, user, pass);
// make a statement with the server
Statement stmt = con.createStatement();
// execute the query and return the result
rs = stmt.executeQuery(query);

System.out.println("Removing old data");

// removing current table data
while (mTableModel.getRowCount() > 0) {
mTableModel.removeRow(0);
}

System.out.println("Adding new data");

// 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);
} //end while loop

System.out.println("Setting table model");
table.setModel(mTableModel);

initComponents();

}catch(Exception e){
e.printStackTrace();
}
finally{
try{
if (rs != null){
rs.close();
}
if (con != null){
con.close();
}
}catch(Exception e){
e.printStackTrace();
}

} // end finally

try {
Thread.sleep(1000L);
}catch(Exception e){
e.printStackTrace();
}

} // end while loop
} // end run
}; // end runnable

Thread thr1 = new Thread(task);
thr1.start();

}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
serverMask = new javax.swing.JLabel();
serverIp = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel14 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel15 = new javax.swing.JLabel();
jLabel16 = new javax.swing.JLabel();
jToggleButton5 = new javax.swing.JToggleButton();
jToggleButton6 = new javax.swing.JToggleButton();
jScrollPane1 = new javax.swing.JScrollPane();
table = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel1.setText("EMS: Cisco 2960 Switch - Interface Status");

jLabel2.setText("ServerIP:");

jLabel3.setText("ServerName:");

jLabel4.setText("Element Name:");

jLabel5.setText("XXXXXXXXXXXXXXX");

serverMask.setText("XXXXXXXXXXXXXXX");

serverIp.setText("XXXXXXXXXXXXXXX");

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 283, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(serverIp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(serverMask, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(81, 81, 81))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(serverIp))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(serverMask))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
);

jLabel8.setBackground(new java.awt.Color(0, 204, 0));
jLabel8.setHorizontalAlignment(javax.swing.SwingCo nstants.CENTER);
jLabel8.setText("ICMP");
jLabel8.setBorder(javax.swing.BorderFactory.create EtchedBorder());
jLabel8.setOpaque(true);

jLabel14.setBackground(new java.awt.Color(0, 204, 0));
jLabel14.setHorizontalAlignment(javax.swing.SwingC onstants.CENTER);
jLabel14.setText("SNMPv3");
jLabel14.setBorder(javax.swing.BorderFactory.creat eEtchedBorder());
jLabel14.setOpaque(true);

jLabel9.setBackground(new java.awt.Color(0, 204, 0));
jLabel9.setHorizontalAlignment(javax.swing.SwingCo nstants.CENTER);
jLabel9.setText("SSH");
jLabel9.setBorder(javax.swing.BorderFactory.create EtchedBorder());
jLabel9.setOpaque(true);

jLabel15.setBackground(new java.awt.Color(0, 204, 0));
jLabel15.setHorizontalAlignment(javax.swing.SwingC onstants.CENTER);
jLabel15.setText("xxx");
jLabel15.setBorder(javax.swing.BorderFactory.creat eEtchedBorder());
jLabel15.setOpaque(true);

jLabel16.setBackground(new java.awt.Color(0, 204, 0));
jLabel16.setHorizontalAlignment(javax.swing.SwingC onstants.CENTER);
jLabel16.setText("ROLE: PRIMARY");
jLabel16.setBorder(javax.swing.BorderFactory.creat eEtchedBorder());
jLabel16.setOpaque(true);

jToggleButton5.setForeground(new java.awt.Color(255, 0, 51));
jToggleButton5.setText("Local Control");

jToggleButton6.setText("View log");

table.setModel(mTableModel);
jScrollPane1.setViewportView(table);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(8, 8, 8)
.addComponent(jLabel1)))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
.addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
.addComponent(jLabel14, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
.addComponent(jLabel15, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)))
.addComponent(jLabel16, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE))
.addGap(140, 140, 140)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
.addComponent(jToggleButton6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToggleButton5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(91, 91, 91))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(11, 11, 11)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(jLabel8)
.addComponent(jLabel14))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(jLabel9)
.addComponent(jLabel15))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jLabel16))
.addGroup(layout.createSequentialGroup()
.addComponent(jToggleButton5)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jToggleButton6)))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(147, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
//SwingUtilities.invokeLater(new Runnable() {
//Runnable task = new Runnable() {

public void run() {
try{
new SwitchInterface().setVisible(true);
}catch(Exception e){e.printStackTrace();}
}
});

//Thread thr1 = new Thread(task);
//thr1.start();
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JToggleButton jToggleButton5;
private javax.swing.JToggleButton jToggleButton6;
private javax.swing.JLabel serverIp;
private javax.swing.JLabel serverMask;
private javax.swing.JTable table;
// End of variables declaration
}

Here's my code for the jtable's customized code:

table = new javax.swing.JTable();

table.setModel(mTableModel);

jScrollPane1.setViewportView(table);



Please help! Thanks