Hi all,

I use the swing timer to move a dialog from one position to another. Here is the code I have use.

// Set the main window display location
    public void setWindowLocation()
    {
        int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
        int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
        
        int frameHeight = this.getHeight();
        int frameWidth = this.getWidth();
                
        int _xValue = (screenWidth - frameWidth);
        int _yValue = (screenHeight - getTrayheight() );
        int _yEnd = (screenHeight - frameHeight - getTrayheight() );
        
        //this.setLocation((screenWidth - frameWidth), (screenHeight - frameHeight - getTrayheight() - WINDOW_SPACE));
        this.setLocation(_xValue, _yValue);
        
        while(_yValue > _yEnd){
            dialogMotion(this, _xValue, _yValue);
            _yValue -= 1;
        }
 
        // this.dispose();
    }
    
// Dialog motion
    private void dialogMotion(final MainDialog mainDialog, final int x, final int y){
        AbstractAction abAction = new AbstractAction() {
            public void actionPerformed(ActionEvent evnt) {
                mainDialog.setLocation(x, y);
            }
        };
        Timer motionTimer = new Timer(300, abAction);
        motionTimer.setRepeats(false);
        motionTimer.start();
    }
    
// Find the taskbar height to place the main frame in lower-right corner
// of the viewable area of the screen.
    private static int getTrayheight(){
        return (Toolkit.getDefaultToolkit().getScreenSize().height -
                 GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height);
    }

I think my code is clear to you. After moving the dialog onto the task bar, I want to dispose it. You can see that at the end of the while loop I've try it.

What happened there is, I can't see the dialog moving. it directly dispose. I add a breakpoint at the while loop and check, it iterate until condition is false. But I can't see the motion. If I comment the dispose code line as in my above code, I can see it moving.

Why is that. Your comment really helpful to me.

I'm worried is that debugging iterate that while loop but it is not visible.

It's quite simple.
The Timer runs in parallel to the code that calls it.
So your method returns immediately, and the dialog is disposed of before the Timer ever completes and starts moving the window (which at that point no longer exists).

This article has been dead for over six months. Start a new discussion instead.