How can I show an inform JDialog before System.exit() in Java Swing? (Closed) -
in simple login window, added action listener login button. on pressing button, program check information user entered, if enter wrong info 3 times program create new jdialog inform user, after program shut down.
my problem jdialog appears blank program shutting down.
here code
btnlogin.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent actionevent) { char[] pass = psspassword.getpassword(); string pss = new string(pass); if (!pss.tostring().equals("admin") || !txtusername.gettext().equals("adminuser")){ if (attempts == 2){ swingutilities.invokelater(new runnable() { @override public void run() { new inform("you entered wrong info 3 times, program shut down"); try { thread.sleep(1000); system.exit(0); } catch (interruptedexception e) { e.printstacktrace(); } } }); }else { swingutilities.invokelater(new runnable() { @override public void run() { new info("wrong info, please check info"); } }); attempts += 1; } }else{ } } });
this inform class:
public class inform extends jdialog{ private jpanel pnlinform; private jlabel lblinform; private jbutton btnok; public inform(string info){ setcontentpane(pnlinform); lblinform.settext(info); pack(); setvisible(true); setdefaultcloseoperation(jdialog.dispose_on_close); setlocationrelativeto(null); btnok.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent actionevent) { dispose(); } }); }
}
my ide intellij idea , use form class create gui, there no declaration elements.
you didnt add components inform
dialog since pnlinform
not constructed , resolvs null
, through contentpane cannot set null.
solutions : construct components (pnlinform,lblinform,btnok) , add them dialog. below can find full functioning code:
package test.q5; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jdialog; import javax.swing.jlabel; import javax.swing.jpanel; public class inform extends jdialog { private jpanel pnlinform=new jpanel(); private jlabel lblinform=new jlabel(); private jbutton btnok=new jbutton("ok"); public inform(string info) { // setcontentpane(pnlinform); add(pnlinform); lblinform.settext(info); pnlinform.add(lblinform); pnlinform.add(btnok); pack(); setvisible(true); setdefaultcloseoperation(jdialog.dispose_on_close); setlocationrelativeto(null); btnok.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent actionevent) { dispose(); } }); } public static void main(string[] args) { new inform("hello"); } }
however , still need formatting message, please feel free add question able help.
Comments
Post a Comment