I'm trying to teach myself Java (for the past week). Anyway, I have a JFrame with a six text areas (each inside a scrollpane). Each text area is populated by running a query on a local access db and posting the results to that area. Currently, to do this, I just have a refresh button that, when clicked, goes to an event which calls all of the individual private voids that query / update the text areas. The source is like this (I'm going to leave some of the bulk out):
This is the code I have been trying... First off I don't see why Thead.sleep is necessary (I'm unsure what it does), and second no matter where I place my timer code, I can't get it to work. I just don't understand where to place it. I'm not sure how to trigger it...I would like it to to be able to click a begin button once and then have it update every 5 minutes or something. I've tried it every way I can think of. Any ideas ?
ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { UpdateProdAreaOne(); UpdateProdAreaTwo(); UpdateProdAreaThree(); UpdateProdAreaFour(); UpdateProdAreaFive(); UpdateProdAreaSix(); } Timer timer = new Timer( 1000 , taskPerformer); //timer.setRepeats(false); timer.start(); Thread.sleep(5000); };
import java.sql.*; import java.util.Set; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.Timer; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import java.awt.event.*; public class DBMonitor extends javax.swing.JFrame { public DBMonitor() { initComponents(); } private void RefreshButtonActionPerformed(java.awt.event.ActionEvent evt) { UpdateProdAreaOne(); UpdateProdAreaTwo(); UpdateProdAreaThree(); UpdateProdAreaFour(); UpdateProdAreaFive(); UpdateProdAreaSix(); } private void UpdateProdAreaOne() { //This is an example..there are 5 others like this //Query //Post results } public static void main(String args[]) { try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(DBMonitor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(DBMonitor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(DBMonitor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(DBMonitor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new DBMonitor().setVisible(true); } }); } //Variable Declarations //...no need to post unless wanted