Swing Timer in Netbeans GUI

Alexandr Shevelev picture Alexandr Shevelev · Apr 8, 2015 · Viewed 8k times · Source

This is my first Swing application so I used NetBeans GUI Builder. There is a Start button to start simulation(two loops: the outer by days and the inner by hours). At each iteration the loop should pause. When you click on the Stop button simulation should stop. I tried to ways: 1)

private void StartActionPerformed(java.awt.event.ActionEvent evt) {                                      
    Timer timer = new Timer(5000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            while(presentDay <= numberOfDays){
                jTextField2.setText(Integer.toString(presentDay));
                for(; presentTime <= 18; presentTime++){
                    jTextField6.setText(Integer.toString(presentTime));

                    StringBuilder res = new StringBuilder();

                    for(Event ev: schedule[presentDay-1].day){
                        if((presentTime <= ev.getTimeFinish()) && (presentTime >= ev.getTimeStart())){
                            res.append(ev);
                            ev.setStatus(1);
                        }
                    }
                    currentEvent.setText(res.toString());
                    // Pause should here
                }
                presentTime = 9;
                presentDay++;
            }
        }
    });
    timer.start();
}

2) Add Timer in main class constructor

public PlanningSystem() {
    initComponents();
    ...
    timer = new Timer(5000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            while(presentDay <= numberOfDays){
                jTextField2.setText(Integer.toString(presentDay));
                for(; presentTime <= 18; presentTime++){
                    jTextField6.setText(Integer.toString(presentTime));

                    StringBuilder res = new StringBuilder();

                    for(Event ev: schedule[presentDay-1].day){
                        if((presentTime <= ev.getTimeFinish()) && (presentTime >= ev.getTimeStart())){
                            res.append(ev);
                            ev.setStatus(1);
                        }
                    }
                    currentEvent.setText(res.toString());
                    // Pause here
                }
                presentTime = 9;
                presentDay++;
            }
        }
    });
}
private void StartActionPerformed(java.awt.event.ActionEvent evt) {                                      

    timer.start();
}

But both ways did not work. I hope you help solve this problem. And sorry for my English :)

====================================================================== After editing :

private void StartActionPerformed(java.awt.event.ActionEvent evt) {                                      
if(presentDay <= numberOfDays){
    jTextField2.setText(Integer.toString(presentDay));
    Timer tim = new Timer (5000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
        if(presentTime <= 18){
            jTextField6.setText(Integer.toString(presentTime));
            StringBuilder res = new StringBuilder();
            for(Event ev: schedule[presentDay-1].day){
                if((presentTime <= ev.getTimeFinish()) && (presentTime >= ev.getTimeStart())){
                    res.append(ev);
                    ev.setStatus(1);
                }
            }
            currentEvent.setText(res.toString());
            presentTime++;
        }
        }
    });            
    tim.start();
    presentTime = 9;
    presentDay++;
}
}

Answer

ControlAltDel picture ControlAltDel · Apr 8, 2015

Your while loop is the problem. You are incrementing presentDay to numberOfDays the first time your timer is called. After that, nothing is ever going to change again.

I believe (though because of the partial code you posted, I can't be sure) that if you take out the while loop, it will work as you desire

Edit: (Better understanding of what you're doing)

Using two timers is wrong. Just use one timer, and update both your presentDay and presentTime there.

Timer tim = new Timer (5000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
           presentTime++;
           if (presentTime == 24) {
             presentTime = 0;
             presentDay++;
           }
           //times how now been set
           //update your GUI here

        }
}