1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Timer with printwriter to server need an answer

Discussion in 'Java' started by notanexpert, May 2, 2011.

  1. notanexpert

    notanexpert New Member

    May 2, 2011
    Likes Received:
    Trophy Points:
    full time student
    Hello all,
    I am building a large piece of software and I haven't had any difficulty until now. I have been on this for 2 days getting nowhere. It might be simple, but I can't figure it out. I have a client/server/database doing all these things. In my client there are many action performs. There is a panel in my program which contains a TextArea with a Clock in it. When the clock is started, the initial value of the clock is displayed in another textArea which I am sending that value to the server. The thing is, my timer is located in my start timer button's action performed event. Even though my t.start() is placed before my try statement that executes my printwriter, the try statement runs before the timer starts making it impossible to read the text in from my text area containing the initial value. I have tried putting my t.start() EVERYWHERE. Not one place allows it to work. Have a look at my snippet of code below and check out my comments;

        Timer t = new javax.swing.Timer(1000, new ClockTickAction());
        public class ClockTickAction implements ActionListener {
        public void actionPerformed(java.awt.event.ActionEvent evt){
                String startstring = "";
                Calendar now = Calendar.getInstance();
                int h = now.get(Calendar.HOUR_OF_DAY);
                int m = now.get(Calendar.MINUTE);
                int s = now.get(Calendar.SECOND);
                 String starttime = "" + h + ":" + m + ":" + s;
                 timerfield.setText(starttime);    // the field my timer is located in visibly
                 boolean checktimerfield= timerfield.getText().isEmpty();
                 boolean checktimerev = timerev1.getText().isEmpty();
                     startstring = timerfield.getText(); // so I can get the initial value of clock
                     timerev1.setText(startstring);  // works great
    // timerev1 contains the text I am trying to send away to the server
    // everything works perfectly, the clock continues ticking and the initial value is placed
        private void startbuttonActionPerformed(java.awt.event.ActionEvent evt) {
             // the button that starts the clock
            t.start(); // starting the clock which seems to happen after the next trycatch
                   outs = new PrintWriter(s.getOutputStream(), true);
                     String employeenumber = clocknamefield.getText();
    // getting an empty string here... which should contain the initial time, which it does, but only after this try statement takes effect.
                      catch (IOException ex) {
                    Logger.getLogger(VideoStoreView.class.getName()).log(Level.SEVERE, null, ex);
    So my server reads the name of the user perfectly, then it prints "initial time is " and there is nothing there. I am trying to figure out how to fix this. I have done 100s of similar things before but never using a Timer like this which has to be my problem. I would really appreciate an answer tonight due to the fact that I am running out of time and am desperate for help.


Share This Page