Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the table like that,

Index | Time | Tem | Hum | Rain | Wind | Level

1

2

Here's the code:
Java
public class MyTable1 extends JFrame {

    MyTable1 mytable;
    Object[][] data;
    JTable tb;
    JScrollPane scp;

    int MAXH=100,MAXC=100;
     public MyTable1() {

            // Header names
            String[] Colnames = {"Index", "Time","Tem","Hum","Rain","Wind","Level"};

          //Col index
            data = new Object[MAXH][MAXC];
            for(int j=0;j<=MAXH-1;j++){
                data[j][0]=j+1;
            }

           // Col Time
             Date tg = new Date();
             final SimpleDateFormat TimeFormat = new SimpleDateFormat(" dd/MM/yyyy HH:mm:ss");

              for(int j=0;j<=MAXH-1;j++){
             data[j][1]=TimeFormat.format(tg.getTime());
              }
           //Col Temperature
            Random rand = new Random(); 
             for(int j=0;j<=MAXH-1;j++){             
                int value = rand.nextInt(30)+10; 
                data[j][2]=value;
            }      
            //Col Humidity      
             for(int j=0;j<=MAXH-1;j++){             
                int value = rand.nextInt(80)+20; 
                data[j][3]=value;
            }   
            //Col Rain
              for(int j=0;j<=MAXH-1;j++){             
                int value = rand.nextInt(2); 
                if (value == 1){
                data[j][4]="Rain";}
                else  data[j][4] = "Not";
            }   

            //Col wind speed     
             for(int j=0;j<=MAXH-1;j++){             
                int value = rand.nextInt(9)+1; 
                data[j][5]=value;
            }    


            tb = new JTable(data, Colnames);

            tb.setFillsViewportHeight(true);
            scp = new JScrollPane(tb);
    ...


Now I want to add automatically every 5 seconds to the jtable the random values like in my code (of course the Index and Time column have to run in order) I know have to use Timer but I don't know how to add in the code. Could anybody show me how to do? Thanks so much!
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900