java - Show a JWindow for a while -


i want show message in jwindow while, tryed use while() , sleep() functions, doesn't work. function should call jwindow ( messagewindow ). there other way show window 2 seconds?

private void showjwindow() {     boolean flag = true;     final messagewindow window = new messagewindow( playername, playerinaction );     window.setvisible( true );      try {         synchronized( window ) {             while( flag ) {                 window.wait( 3000 );                 flag = false;                 window.setvisible( false );             }         }      } catch( interruptedexception ie ) {         thread.currentthread().interrupt();     } } 

here short example using swing timer.

import java.awt.event.actionevent; import java.awt.event.actionlistener;  import javax.swing.jbutton; import javax.swing.jdialog; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.swingutilities; import javax.swing.timer;  public class testgui {     public testgui() {         final jframe frame = new jframe("test");         jbutton press = new jbutton("press me");         frame.setdefaultcloseoperation(jframe.dispose_on_close);         frame.add(press);         press.addactionlistener(new actionlistener() {              @override             public void actionperformed(actionevent arg0) {                 final jdialog dialog = new jdialog();                 dialog.add(new jlabel("here 2 seconds"));                 dialog.pack();                 dialog.setlocationrelativeto(null);                 dialog.setvisible(true);                 timer timer = new timer(2000, new actionlistener() {                      @override                     public void actionperformed(actionevent e) {                         dialog.setvisible(false);                         dialog.dispose();                     }                 });                 timer.setrepeats(false);                 timer.start();             }         });         frame.pack();         frame.setlocationrelativeto(null);         frame.setvisible(true);     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {              @override             public void run() {                 new testgui();             }         });     } } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -