java - How to close JOptionPane automatically? -


i have thread running , in time while thread working want display joptionpane.showmessagedialog saying application working , after thread stopped, joptionpane close automatically , ok button deactivated whole time. main code :

class index { public static void main(string args[]) {    newthread ob1 = new newthread("one");    newthread ob2 = new newthread("two");    newthread ob3 = new newthread("three");     system.out.println("thread 1 alive: "+ ob1.t.isalive());     system.out.println("thread 2 alive: "+ ob2.t.isalive());     system.out.println("thread 3 alive: "+ ob3.t.isalive());    while (ob1.t.isalive()){     joptionpane.showmessagedialog(null,"thread 1 alive");    }     system.out.println("thread 1 alive: "+ ob1.t.isalive());     system.out.println("thread 2 alive: "+ ob2.t.isalive());     system.out.println("thread 3 alive: "+ ob3.t.isalive());     system.out.println("main thread exiting."); }} 

and when run joptionpane display won't close automatically unless pressed ok button.

class newthread implements runnable { string name;  thread t;     newthread(string threadname) {         this.name = threadname;         this.t = new thread(this, name);         system.out.println("new thread: " + t);         this.t.start(); // start thread     }  public void run() {     try {     for(int = 5; > 0; i--) {         system.out.println(name + ": " + i);         thread.sleep(1000);         }         } catch (interruptedexception e) {         system.out.println(name + " interrupted.");}         system.out.println(name + " exiting."); }} 

  • create own dialog without buttons...make sure set defaultcloseoperation do_nothing
  • use progressmonitor

progressmonitor example...

enter image description here

import java.awt.borderlayout; import java.awt.eventqueue; import java.beans.propertychangeevent; import java.beans.propertychangelistener; import javax.swing.jframe; import javax.swing.progressmonitor; import javax.swing.swingworker; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class testprogress {      public static void main(string[] args) {         new testprogress();     }      public testprogress() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                 }                  new backgroundworker().execute();              }          });     }      public class backgroundworker extends swingworker<void, void> {          private progressmonitor monitor;          public backgroundworker() {             addpropertychangelistener(new propertychangelistener() {                 @override                 public void propertychange(propertychangeevent evt) {                     if ("progress".equalsignorecase(evt.getpropertyname())) {                         if (monitor == null) {                             monitor = new progressmonitor(null, "processing", null, 0, 99);                         }                         monitor.setprogress(getprogress());                     }                 }              });         }          @override         protected void done() {             if (monitor != null) {                 monitor.close();             }         }          @override         protected void doinbackground() throws exception {             (int index = 0; index < 100; index++) {                 setprogress(index);                 thread.sleep(125);             }             return null;         }     } } 

dialog example

enter image description here

import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import java.beans.propertychangeevent; import java.beans.propertychangelistener; import javax.swing.jdialog; import javax.swing.jlabel; import javax.swing.jprogressbar; import javax.swing.swingworker; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class testprogress {      public static void main(string[] args) {         new testprogress();     }      public testprogress() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                 }                  new backgroundworker().execute();              }          });     }      public class backgroundworker extends swingworker<void, void> {          private jprogressbar pb;         private jdialog dialog;          public backgroundworker() {             addpropertychangelistener(new propertychangelistener() {                 @override                 public void propertychange(propertychangeevent evt) {                     if ("progress".equalsignorecase(evt.getpropertyname())) {                         if (dialog == null) {                             dialog = new jdialog();                             dialog.settitle("processing");                             dialog.setlayout(new gridbaglayout());                             dialog.setdefaultcloseoperation(jdialog.do_nothing_on_close);                             gridbagconstraints gbc = new gridbagconstraints();                             gbc.insets = new insets(2, 2, 2, 2);                             gbc.weightx = 1;                             gbc.gridy = 0;                             dialog.add(new jlabel("processing..."), gbc);                             pb = new jprogressbar();                             gbc.gridy = 1;                             dialog.add(pb, gbc);                             dialog.pack();                             dialog.setlocationrelativeto(null);                             dialog.setvisible(true);                         }                         pb.setvalue(getprogress());                     }                 }              });         }          @override         protected void done() {             if (dialog != null) {                 dialog.dispose();             }         }          @override         protected void doinbackground() throws exception {             (int index = 0; index < 100; index++) {                 setprogress(index);                 thread.sleep(125);             }             return null;         }     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -