Swing components not reflecting new data

G

GVR_Mike

Wondering if someone can help me figure out why this gui isn't
updating the JTextFields. It displays the gui and a Timer reruns
queries every 5 seconds and reassigns the text in the JTextFields to
the new values from the queries. The problem is that it isn't updating
the text fields in the gui, they always display the default values.
What am I doing wrong? (I left out the code that initializes the
components, don't think that has anything to do with it). I'm using
Netbeans and swing. Plus any other suggestions on how to do this
better are well appreciated. Thanks!!!

import java.sql.SQLException;
import javax.swing.Timer;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Graphics;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class FmsTvScreen extends javax.swing.JFrame {

private Timer timer;
private Connection oracleConn;
private String schema;
private PreparedStatement pstmtAlarmQuery, pstmtPhoneQuery;
private ResultSet rsAlarmData, rsPhoneData;

/** Creates new form FmsTvScreen */
public FmsTvScreen() {
initComponents();
}

private void initComponents() {...} //collapsed code

private void clearDBConnection() {
if (this.oracleConn != null){
try {
this.oracleConn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
this.oracleConn = null;
}

public void setConnection() throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
this.oracleConn = DriverManager.getConnection(
"jdbc:eek:dbc:GVR2Test", "USER", "PASSWORD");
}

public void declarePreparedStatements() throws SQLException {
this.pstmtAlarmQuery = this.oracleConn.prepareStatement(
"SELECT AAM_CATETORY, AAM_COUNT, AAM_OLDEST_SEC,
AAM_AVE_SEC, " +
"AAM_TOTAL_SEC, AAM_DAILY_SL, AAM_HOURLY_SL " +
"FROM VRSC.ALARM_ACTIVITY_MONITOR " +
"ORDER BY AAM_INDEX");
this.pstmtPhoneQuery = this.oracleConn.prepareStatement(
"SELECT RSS_CALLS_IN_QUE, RSS_MAX_SECONDS,
RSS_CALL_VOLUME, " +
"RSS_SERVICE_LEVEL " +
"FROM VRSC.RTB_SERVICE_SUMMARY " +
"WHERE RSS_CATETORY IN ( 'CMS', 'FMS' ) " +
"ORDER BY RSS_CATETORY DESC");
}

public void setAlarmResultSet(ResultSet rs) {
this.rsAlarmData = rs;
}

public void setPhoneResultSet(ResultSet rs) {
this.rsPhoneData = rs;
}

public ResultSet runAlarmQuery() throws SQLException {
return this.pstmtAlarmQuery.executeQuery();
}

public ResultSet runPhoneQuery() throws SQLException {
return this.pstmtPhoneQuery.executeQuery();
}

public void populateTvFields() throws SQLException {
/*Grabs the data from the ResultSets and populates
* the TV Screen data fields. The column numbers
* are as follows:
* 2 = AAM_COUNT
* 3 = AAM_OLDEST_SEC
* 4 = AAM_AVE_SEC
* 5 = AAM_DAILY_SL
* 6 = AAM_HOURLY_SL
*/
rsAlarmData.next();
this.liCount.setText(rsAlarmData.getString(2));
this.liOldest.setText(rsAlarmData.getString(3));
this.liAverage.setText(rsAlarmData.getString(4));
this.liDailySL.setText(rsAlarmData.getString(5));
this.liHourlySL.setText(rsAlarmData.getString(6));

rsAlarmData.next();
this.tgCount.setText(rsAlarmData.getString(2));
this.tgOldest.setText(rsAlarmData.getString(3));
this.tgAverage.setText(rsAlarmData.getString(4));
this.tgDailySL.setText(rsAlarmData.getString(5));
this.tgHourlySL.setText(rsAlarmData.getString(6));

rsAlarmData.next();
this.intTLSCount.setText(rsAlarmData.getString(2));
this.intTLSOldest.setText(rsAlarmData.getString(3));
this.intTLSAverage.setText(rsAlarmData.getString(4));
this.intTLSDailySL.setText(rsAlarmData.getString(5));
this.intTLSHourlySL.setText(rsAlarmData.getString(6));

rsAlarmData.next();
this.intFMSCount.setText(rsAlarmData.getString(2));
this.intFMSOldest.setText(rsAlarmData.getString(3));
this.intFMSAverage.setText(rsAlarmData.getString(4));

rsAlarmData.next();
//This is the Internal SMS row, not used for this program

rsAlarmData.next();
this.openCount.setText(rsAlarmData.getString(2));
this.openOldest.setText(rsAlarmData.getString(3));
this.openAverage.setText(rsAlarmData.getString(4));

rsAlarmData.next();
this.miscCategoryLabel.setText(rsAlarmData.getString(1));
this.miscCategoryData.setText(rsAlarmData.getString(2));
}

public static void main(String args[]) {
FmsTvScreen fmsTv = new FmsTvScreen();
try {
//fmsTv.getDBConnection();
QueryRunnable queryRunner = new QueryRunnable();
EventQueue.invokeLater(queryRunner);
} catch (Exception e) {
e.printStackTrace();
} finally {
fmsTv.clearDBConnection();
}
}

// Variables declaration
private javax.swing.JTextField cmsCallsInQ;
private javax.swing.JTextField cmsOldest;
private javax.swing.JTextField cmsSL;
private javax.swing.JTextField cmsTotalVol;
private javax.swing.JTextField fmsCallsInQ;
private javax.swing.JTextField fmsOldest;
private javax.swing.JTextField fmsSL;
private javax.swing.JTextField fmsTotalVol;
private javax.swing.JTextField intFMSAverage;
private javax.swing.JTextField intFMSCount;
private javax.swing.JTextField intFMSOldest;
private javax.swing.JTextField intTLSAverage;
private javax.swing.JTextField intTLSCount;
private javax.swing.JTextField intTLSDailySL;
private javax.swing.JTextField intTLSHourlySL;
private javax.swing.JTextField intTLSOldest;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
private javax.swing.JLabel jLabel17;
private javax.swing.JLabel jLabel18;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JTextField liAverage;
private javax.swing.JTextField liCount;
private javax.swing.JTextField liDailySL;
private javax.swing.JTextField liHourlySL;
private javax.swing.JTextField liOldest;
private javax.swing.JTextField miscCategoryData;
private javax.swing.JLabel miscCategoryLabel;
private javax.swing.JTextField openAverage;
private javax.swing.JTextField openCount;
private javax.swing.JTextField openOldest;
private javax.swing.JTextField tgAverage;
private javax.swing.JTextField tgCount;
private javax.swing.JTextField tgDailySL;
private javax.swing.JTextField tgHourlySL;
private javax.swing.JTextField tgOldest;
// End of variables declaration

}

class QueryRunnable implements Runnable {

private static QueryActionListener queryListener;
private static final int delay = 5000;

public void run() {
FmsTvScreen fmsTv = new FmsTvScreen();
fmsTv.setVisible(true);
queryListener = new QueryActionListener();
new Timer(delay, queryListener).start();
}
}

class QueryActionListener implements ActionListener {

public void actionPerformed (ActionEvent ae){
System.out.println("still running...");
FmsTvScreen fmsTv = new FmsTvScreen();
try {
fmsTv.setConnection();
fmsTv.declarePreparedStatements();
fmsTv.setAlarmResultSet(fmsTv.runAlarmQuery());
fmsTv.setPhoneResultSet(fmsTv.runPhoneQuery());
fmsTv.populateTvFields();

} catch (Exception e) {
e.printStackTrace();
}
}
}
 
G

GVR_Mike

Duh, sorry, I figured it out. I was creating a new FmsTvScreen object
and trying to update the variables there, meanwhile the one that was
displaying on the screen wasn't getting updated. I have it working
now, sorry to bother you all!!!
 
C

Chase Preuninger

This advice is not related to your question, but you should find a
better way (possibly an array) of storing all those text fields.
 
C

Chase Preuninger

You could also try to repaint, and put in a literal string into one of
them to see if that works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top