swing - I need help on a few things on Java JFrame -
i have created progress code , want close automatically after going 100% , open new frame text in frame. how do this?
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.sql.*; import java.util.*; class progress extends jframe implements actionlistener { jprogressbar pb; jbutton b1; progress() { super("progress"); setlayout(null); b1 = new jbutton("start"); b1.setbackground(color.light_gray); pb = new jprogressbar(1,100); pb.setvalue(0); pb.setstringpainted(true); pb.setforeground(color.green); pb.setbackground(color.pink); b1.setbounds(20, 20, 80, 25); pb.setbounds(110, 20, 200, 25); pb.setvisible(false); add(b1); add(pb); b1.addactionlistener(this); setresizable(false); setdefaultcloseoperation(exit_on_close); } public void actionperformed(actionevent e) { int i=0; if(e.getsource()==b1) { pb.setvisible(true); try { while(i<=100) { thread.sleep(50); pb.paintimmediately(0, 0, 200, 25); pb.setvalue(i); i++; } } catch(exception e1) { system.out.print("caughted exception ="+e1); } } } public static void main(string arg[]) { logindemo m=new logindemo(); m.setsize(400,250); m.setvisible(true); dimension dimension = toolkit.getdefaulttoolkit().getscreensize(); int x = (int) ((dimension.getwidth() - m.getwidth()) / 2); int y = (int) ((dimension.getheight() - m.getheight()) / 2); m.setlocation(x, y); } }
this code progress bar.
so thing before answer.
progressbar
in code while progress increasing rest of application frozen. if want avoid it, it's have progress bar code in thread. remember use swingworker instead of classic threads.
also read thing andrew thompson mentioned in comments.
to "close" or rather hide main frame u can call 1 of these methods
setvisible(false);
or
dispose();
to open new window/frame or whatever, have put after while loop. can check code below, add method class , call after loop.
private void dialogmessage() { object[] options = {"ok"}; int result = joptionpane.showoptiondialog(this, "done!", "", joptionpane.ok_option, joptionpane.question_message, null, options, options[0]); if (result == joptionpane.ok_option) { system.exit(0); } }
to read more joptionpane check out link how make dialogs
Comments
Post a Comment