views:

71

answers:

2

I am using Java Swing to create a JDialog, and i am trying to create a Show/Hide details button to show/hide a report at the bottom of this JDialog.

It works fine for me, but i want to do this with time, to add a small animation effect while showing/hiding the report, i have used TimerTask but it's just showing the report directly without any slow motion ... Here's my current code :

private void showHideDetailsButtonActionPerformed() {
    final MyDialog myDialog = this;
    int fullHeight = this.getHeight();
    int smallHeight = this.getHeight()/2 - 4;
    this.setSize( this.getWidth(), smallHeight );  // By default hide the report.

    if( this.getHeight() == smallHeight ) {  // Show details.
        new Timer().schedule(
            new java.util.TimerTask() {
                @Override
                public void run() {
                    while( myDialog.getHeight() < fullHeight ) {
                        myDialog.setSize( myDialog.getWidth(), myDialog.getHeight() + 1 );
                        System.out.println( myDialog.getHeight() );
                    }
                }
            }, 
            800
        );
    }
}
+1  A: 

Use javax.swing.Timer, not java.util.Timer... or use Trident.

Istao
Perfect ... javax.swing.Timer is so simple and does the job.
Brad
+1  A: 

Trying calling myDialog.repaint() after setting the size in the TimerTask's run() method.

Michael Angstadt
Thanks, but it still shows the report directly after the Timer interval ends.
Brad