0

I'm new into programming and I'm having some trouble with this.

The problem is, I'm using the Swing palette to create an assignment where I'm using a JDialog to display a timer at the same time of another frame, when I dispose this frame to change to another and return to the previous one the timer in the JDialog overlaps the first one that was running, and I couldn't managed to fix it.

Here's the code.

MAIN

public static void main(String[] args) {
    Panel0 screen=new Panel0();
    screen.setTitle("");
    screen.setLocationRelativeTo(screen);
    screen.setVisible(true);

}

1st FRAME

public class Panel0 extends javax.swing.JFrame {
Panel s=new Panel();

private void fisica1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    s.time();
    s.setTitle("FISIC I");
    s.setLocationRelativeTo(s);
    s.setVisible(rootPaneCheckingEnabled);
    s.dialog.setVisible(rootPaneCheckingEnabled);
    dispose();
}          

2nd FRAME

public class Panel extends javax.swing.JFrame {    

private void EndActionPerformed(java.awt.event.ActionEvent evt) {                                    
    dialog.dispose();
    dialog.setDefaultCloseOperation(0);

    Panel0 pan=new Panel0();
    pan.setLocationRelativeTo(p1);
    pan.setVisible(rootPaneCheckingEnabled);
    dispose();

}   

void time(){        

    t=new Timer(1,new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (startTime<0) {
                startTime=System.currentTimeMillis();                    
            }                
               long now = System.currentTimeMillis();
               long clockTime = now - startTime;                
            if (clockTime >= duration) {
                    clockTime = duration;
                    t.stop();
            }                
             SimpleDateFormat sdf=new SimpleDateFormat("mm:ss:SS");
                              clock.setText(sdf.format(duration-clockTime));                                  


       }
    });        
    t.setInitialDelay(0);            
                if (!t.isRunning()) {
                    startTime = -1;
                    t.start();
                }
}

I omitted the inizialization of the Timer and such, because I don't think that's the problem.

To clarify something: Once I close the 2nd frame the 1st opens and gives me options to repeat this process over and over, and everytime the JDialog named "dialog" overlaps with its data (you can see the numbers of the clock overlaping).

Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
Daniel L
  • 1
  • 1
  • 5
    *"Here's the code."* Code is **missing!** For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 08 '16 at 05:34

1 Answers1

0

dispose() does not means that you will "destroy" the object or clear its state. It means that you will release graphics resources attached to that frame (low level window handle and stuff). It still can be reused with setVisible(true)

I assume that you want to reuse our popup - this is just fine, but I think that you are forgetting to stop the "disposed" timer thus every new timer you create on action will be exposed to so called "racing conditions".

Timers are simple background task and they must be stopped explicitly - it will not be done by itself.

Every call to s.time(); starts new timer without stopping previous one.

Simply speaking: you have multiple timers updating the same text field.

Solution: Stop previous timer before running new OR restart previous timer.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99