Thursday, 15 July 2010

java - Trying to execute file every x seconds -



java - Trying to execute file every x seconds -

please find updated code repeats every 10 seconds. issue creates new gui on screen every 10 seconds rather updating info every 10 seconds. please can advise

package learningfromscrach; import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.ioexception; import java.util.logging.level; import java.util.logging.logger; import javax.swing.*; import javax.swing.border.titledborder; import org.jsoup.jsoup; import org.jsoup.nodes.document; import org.jsoup.select.elements; import javax.swing.jtextarea; import com.sun.java.swing.*; import javax.swing.table.*; import java.awt.flowlayout; import java.text.decimalformat; import java.util.date; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.table.defaulttablemodel; import org.apache.log4j.basicconfigurator; import static org.quartz.datebuilder.evenminutedate; import org.quartz.job; import static org.quartz.jobbuilder.newjob; import org.quartz.jobdetail; import org.quartz.jobexecutioncontext; import org.quartz.jobexecutionexception; import org.quartz.scheduler; import org.quartz.schedulerexception; import org.quartz.schedulerfactory; import static org.quartz.simpleschedulebuilder.simpleschedule; import org.quartz.simpletrigger; import org.quartz.trigger; import static org.quartz.triggerbuilder.newtrigger; import org.quartz.impl.stdschedulerfactory; import java.util.date; import java.util.timer; import java.util.timertask; import java.awt.toolkit; public class learningfromscrach extends jframe implements job { toolkit toolkit; timer timer; //declare parts create gui private jlabel textjlabel; private jpanel paneljlabel;//jlabel parameter in java private titledborder paneljborder; defaulttablemodel model; jtable table; public learningfromscrach(int seconds) //tell java initiate create interface //this passed timer every 10 seconds { createuserinterface();//create method private void createuserinterface//aframe parameter has match private void } class learningtask extends timertask { public void run() { system.out.format("timer task finished..!%n"); //system.exit(0); // terminate timer thread } } private void createuserinterface() //all parts create userinterface {//from here container contentpane = getcontentpane(); contentpane.setlayout( null);// responsible setting positioning , size of components settitle("cashout prices");//settitle java parameter dimension screensize = toolkit.getdefaulttoolkit().getscreensize(); setsize((int) (screensize.width/6), (int) (screensize.height/1.1));//cast int width setvisible(true); //makes java application show system.out.format("timer task started at:"+new date()); string url = "http://bmreports.com/bsp/additional/soapfunctions.php?element=sysprice&dt=nrt"; document doc = null; seek { doc = jsoup.connect(url).get(); } grab (ioexception ex) { logger.getlogger(learningfromscrach.class.getname()).log(level.severe, null, ex); } elements periodparagraphs; elements sspparagraphs; elements sbpparagraphs; periodparagraphs = doc.select("sp");//counts number of ssp paragraphs in entire document sspparagraphs = doc.select("ssp");//counts number of ssp paragraphs in entire document sbpparagraphs = doc.select("sbp");//counts number of sbp paragraphs in entire document //decimalformat df = new decimalformat("#.###"); //df.format(0.912385); string[] numbers1; numbers1 = periodparagraphs.text().split(" "); string[] numbers; numbers = sspparagraphs.text().split(" "); string[] numbers0; numbers0 = sbpparagraphs.text().split(" "); //string str = "1234"; //int num = integer.parseint(str); int tablerows; if (numbers0.length > numbers.length && numbers0.length > numbers1.length) { tablerows = numbers0.length; } else if (numbers.length > numbers0.length && numbers.length > numbers1.length) { tablerows = numbers.length; } else { tablerows = numbers1.length; } //model = new defaulttablemodel(col, 90);//50 number of rows --you don't seem need object[][] info = new string[tablerows][3];//3 number of columns (int x = 0; x < tablerows; x++ ) { seek { data[x][0] = numbers1[x]; } grab (indexoutofboundsexception e) { data[x][0] = " ";//error } seek { data[x][1] = numbers[x]; } grab (indexoutofboundsexception e) { data[x][1] = " "; } seek { data[x][2] = numbers0[x]; } grab (indexoutofboundsexception e) { data[x][2] = " "; } } //header.setbackground(color.yellow); // model = new defaulttablemodel(col,90);//50 number of rows string col[] = {"sbp","ssp","period"}; table=new jtable(data,col){@override public boolean iscelleditable(int arg0, int arg1) { homecoming false; }}; jtableheader header = table.gettableheader(); header.setbackground(color.yellow); contentpane.add(table); //table.setvalueat(sbpparagraphs.text(),0,0); //first number moves placing downwards 2 rows//2nd number next cclumn , on //table.setvalueat("fgfg",0,0); table.setsize(screensize.width/2, (int) (screensize.height/1.1)); table.setbounds(16,50,400,2000); table.setlayout(null); table.setvisible(true); //add(table.gettableheader(), borderlayout.north); // table.setlayout(new borderlayout()); //add(table, borderlayout.center); setdefaultcloseoperation(exit_on_close); } //to hear refers contentpane /** * @param args command line arguments */ public static void main(string[] args) throws schedulerexception, interruptedexception { // todo code application logic here // basicconfigurator.configure(); new learningfromscrach(1); system.out.format("task scheduled.. wait 5 sec see next message..%n"); } //next step understand how info panel @override public void execute(jobexecutioncontext jec) throws jobexecutionexception { _log.info("hello world! - " + new date()); throw new unsupportedoperationexception("not supported yet."); //to alter body of generated methods, take tools | templates. } }

you have 1 class, learningfromscrach. within have main function. within main function create another instance of learningfromscrach passed timer run every 10 seconds. when runs in 10 seconds, creates yet instance of learningfromscrach, , on.

you don't need re-implement learningfromscrach every 10 seconds. want info update every 10 seconds. should able set timer on update function run every 10 seconds. (i can't code examples right now, investigate , see how far get.)

edit: refactored project, eliminating unused elements , adding methods , jscrollpane. hope helps.

import java.awt.*; import java.io.ioexception; import java.util.logging.level; import java.util.logging.logger; import javax.swing.*; import org.jsoup.jsoup; import org.jsoup.nodes.document; import org.jsoup.select.elements; import javax.swing.table.*; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.table.defaulttablemodel; import java.util.timer; import java.util.timertask; import java.awt.toolkit; import java.awt.borderlayout; public class learningfromscrach extends jframe { toolkit toolkit; timer timer = new timer(); defaulttablemodel model; jtable table; string url = "http://bmreports.com/bsp/additional/soapfunctions.php?element=sysprice&dt=nrt"; string col[] = {"sbp","ssp","period"}; jscrollpane scrollpane; public learningfromscrach() //tell java initiate create interface //this passed timer every 10 seconds { createuserinterface(); } private void createuserinterface() //all parts create userinterface {//from here setlayout( new borderlayout()); settitle("cashout prices");//settitle java parameter setvisible(true); //makes java application show table = createtable(url,col); scrollpane = new jscrollpane(table, scrollpaneconstants.vertical_scrollbar_always, scrollpaneconstants.horizontal_scrollbar_always); add(scrollpane,borderlayout.center); pack(); setsize(getwidth(), 200); //initial size setdefaultcloseoperation(exit_on_close); int delay = 5000; //5 seconds int period = 5000; //5 seconds timer.scheduleatfixedrate(new timertask() { public void run() { refreshtable(url, col); system.out.format("task run.."); } }, delay, period); } private object[][] gettableinfo(string url) { document doc = null; seek { doc = jsoup.connect(url).get(); } grab (ioexception ex) { logger.getlogger(learningfromscrach.class.getname()).log(level.severe, null, ex); } elements periodparagraphs; elements sspparagraphs; elements sbpparagraphs; periodparagraphs = doc.select("sp");//counts number of ssp paragraphs in entire document sspparagraphs = doc.select("ssp");//counts number of ssp paragraphs in entire document sbpparagraphs = doc.select("sbp");//counts number of sbp paragraphs in entire document string[] numbers1; numbers1 = periodparagraphs.text().split(" "); string[] numbers; numbers = sspparagraphs.text().split(" "); string[] numbers0; numbers0 = sbpparagraphs.text().split(" "); int tablerows; if (numbers0.length > numbers.length && numbers0.length > numbers1.length) { tablerows = numbers0.length; } else if (numbers.length > numbers0.length && numbers.length > numbers1.length) { tablerows = numbers.length; } else { tablerows = numbers1.length; } object[][] info = new string[tablerows][3];//3 number of columns (int x = 0; x < tablerows; x++ ) { seek { data[x][0] = numbers1[x]; } grab (indexoutofboundsexception e) { data[x][0] = " ";//error } seek { data[x][1] = numbers[x]; } grab (indexoutofboundsexception e) { data[x][1] = " "; } seek { data[x][2] = numbers0[x]; } grab (indexoutofboundsexception e) { data[x][2] = " "; } } homecoming data; } private jtable createtable(string url, string[] cols) { jtable temptable=new jtable(gettableinfo(url),col) { @override public boolean iscelleditable(int arg0, int arg1) { homecoming false; } }; jtableheader header = temptable.gettableheader(); header.setbackground(color.yellow); temptable.setlayout(null); temptable.setvisible(true); homecoming temptable; } private void refreshtable(string url, string[] cols) { object[][] info = gettableinfo(url); (int row = 0; row < info.length; row++) { (int column = 0; column < cols.length; column++) { table.setvalueat(info[row][column],row, column); } } } public static void main(string[] args) throws interruptedexception { // todo code application logic here // basicconfigurator.configure(); new learningfromscrach(); system.out.format("task scheduled.. wait 5 sec see next message..%n"); }

}

java quartz-scheduler

No comments:

Post a Comment