开发者

How to create Show/Hide details button?

开发者 https://www.devze.com 2023-01-05 06:41 出处:网络
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.

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() {
              开发者_如何学JAVA  @Override
                public void run() {
                    while( myDialog.getHeight() < fullHeight ) {
                        myDialog.setSize( myDialog.getWidth(), myDialog.getHeight() + 1 );
                        System.out.println( myDialog.getHeight() );
                    }
                }
            }, 
            800
        );
    }
}


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号