Results 1 to 8 of 8
  1. #1
    abelah is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Smile Refresh window to get new data from database

    hi all..

    I have this big problem which i tried to solve it but till now dont see any way..

    I want to make the code below (GMSdata()) to be refresh every second so that it can retrieve any new value stored in the database.

    I tried to use SwingWorker but don't know how to implement SwingWorker into this code.

    TQ
    Kindly help me..

    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileWriter;
    import java.sql.*;
    import javax.swing.JOptionPane;


    public class GMSapps extends javax.swing.JFrame {
    public String ans;

    private void GMSdata(){

    //do things here

    }

    private void GMSadvice(){}

    private void GMSgraph(){}

    public GMSapps() {
    initComponents();
    GMSdata();
    GMSadvice();
    GMSgraph();
    }

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new GMSapps().setVisible(true);

    }
    });
    }
    }

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default Timer

    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Smile separate thread for updating and main thread for GUI.

    Java Code:
    public class GMSapps extends javax.swing.JFrame {
    public String ans;
    //........
    public static void main(String[] args)
    {
        DataContainer data(args[0]);//
        GMSdata dataRunner = new GMSdata(data);
        dataRunner.start();
        JFrame jf = new JFrame(dataRunner);
        return;
    }
    }
    // Encapsulate 
    class GMSdata extends Thread{
    private DataContainer dataContainerReference;
    GMSdata(DataContainer dataContainer){
    dataContainerReference = dataContainer;
    }
    public void run(){
       // .... do stuff here
    }
    } 
    class DataContainer{
    public DataContainer(String s){
        //... code omitted 
    }
    Modifiy for SwingWorker, contrived example for get-going.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ya, for me threads are the most suitable thing here. I think Nicol, your code segment gives a clear idea for our thread starter.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I get a bit nervous when I see one class import Swing, AWT, and such and also import File, sql, and non-GUI related stuff as this breaks a cardinal rule of programming of separating things that don't belong together. I strongly recommend that you get your database code out of your GUI and same for any non-database file I&O as well.

    Your GUI code should simply create the GUI, and then have public methods for allowing your database to publish its information into the GUI as it changes.

    A SwingWorker would work well here. In your doInBackground method you could have a utility timer or a Thread.sleep() loop that then requests information from the database. Once the code gets the info from the database you call publish with your database information packaged into a class, and the process method then calls the public GUI methods to update your view.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    I get a bit nervous when I see one class import Swing, AWT, and such and also import File, sql, and non-GUI related stuff as this breaks a cardinal rule of programming of separating things that don't belong together. I strongly recommend that you get your database code out of your GUI and same for any non-database file I&O as well.
    Lol I'm confusing here. What type of separation are you talking about here? Just separate on relative import or separate in different files, based on OOP concepts?

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default separation of working code

    Quote Originally Posted by Eranga View Post
    Lol I'm confusing here. What type of separation are you talking about here? Just separate on relative import or separate in different files, based on OOP concepts?
    Fubarable provides the details - what I left out as I had to go to other work - and is important for original poster to build on what I sketched. One can move code to differing compilation units ( files ) then defeat that with imports and declarations of class variables from other compilation units, say for example:

    { file one }
    public class DataStorage{}
    { file two }
    public class DataWorker{}
    { file three }
    public class DataDisplay{}
    { main class }
    DataStorage dataStorage = new ( new DataWorker( new DataDisplay()));//

    and in so doing mesh things together in a way and manner that are not advisable due to ( perhaps ) compiler optimizations.

    Further description from original poster would be needed before it could be sorted out.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. how to refresh data of the JTable
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-17-2008, 12:01 PM
  2. [SOLVED] Getting Data from a database
    By AJG in forum New To Java
    Replies: 8
    Last Post: 04-04-2008, 12:52 PM
  3. refresh a different window
    By marceldupont in forum AWT / Swing
    Replies: 3
    Last Post: 03-22-2008, 02:44 AM
  4. Replies: 0
    Last Post: 03-11-2008, 04:55 PM
  5. How to query data from database using SSL
    By mano in forum New To Java
    Replies: 0
    Last Post: 08-02-2007, 05:30 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
  •