swing - Progress bar in Java -
i have got form in java (swing) loading large amount of data database. want display progress bar while program gets loaded.
how can it?
the code follows:
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.sql.*; import java.applet.*; import java.util.*; import java.awt.color; public class bookshow extends jframe implements mouselistener, actionlistener { jpanel p1, p2, p3; jlabel l1, l2, l3, l4; imageicon image; jbutton b[] = new jbutton[84]; jbutton btnocc, btnbook, btnsel; resultset rs; private jmenubar menubar = new jmenubar(); private jmenuitem exit, home, save; private jmenu filemenu, gotomenu; public int cnt = 0, x = 150, y = 90, i, j, put = 0, k = 0, avail = 1, point, x1, y1, count = 0, quan; public static int prev_click = -1, xpos, ypos; public static int prev[] = new int[5]; public static int pos[][] = new int[5][2]; public string movname, movdate, movtime; public bookshow() { avail = 1; try { string sql = "select * pass"; string url = "jdbc:odbc:tbs"; connection con = drivermanager.getconnection(url); statement s = con.createstatement(); rs = s.executequery(sql); while (rs.next()) { movname = rs.getstring("movname"); movdate = rs.getstring("movdate"); movtime = rs.getstring("movtime"); quan = integer.parseint(rs.getstring("quantity")); } } catch (exception ex) { system.out.println(ex); } try { string sql = "delete pass"; string url = "jdbc:odbc:tbs"; connection con = drivermanager.getconnection(url); statement s = con.createstatement(); s.executeupdate(sql); con.close(); } catch (exception ex) { system.out.println(ex); } settitle("select ticket"); setdefaultcloseoperation(exit_on_close); p1 = new jpanel(); getcontentpane().add(p1); p1.setlayout(null); p1.setbackground(color.white); setjmenubar(menubar); filemenu = new jmenu("file"); menubar.add(filemenu); exit = new jmenuitem("exit"); exit.addactionlistener(this); filemenu.add(exit); exit.setaccelerator(keystroke.getkeystroke('e', event.ctrl_mask)); save = new jmenuitem("save"); save.addactionlistener(this); filemenu.add(save); save.setaccelerator(keystroke.getkeystroke('s', event.ctrl_mask)); gotomenu = new jmenu("go to"); menubar.add(gotomenu); home = new jmenuitem("home"); //exit.addactionlistener(this); gotomenu.add(home); home.setaccelerator(keystroke.getkeystroke('h', event.ctrl_mask)); p2 = new jpanel(); p2.setlayout(null); p2.setbackground(color.lightgray); p2.setbounds(100, 50, 500, 20); p1.add(p2); l1 = new jlabel("gold"); l1.setbounds(240, 0, 100, 20); p2.add(l1); l1.setbackground(color.red); try { string sql = "select * book"; string url = "jdbc:odbc:tbs"; connection con = drivermanager.getconnection(url); statement s = con.createstatement(); rs = s.executequery(sql); while (rs.next()) { string getname = rs.getstring("movname"); string getdate = rs.getstring("movdate"); string gettime = rs.getstring("movtime"); if (movname.equalsignorecase(getname) && movdate.equals(getdate) && movtime.equals(gettime)) { put = 1; break; } } } catch (exception ex) { system.out.println(ex); } (i = 1; <= 4; i++) { (j = 1; j <= 14; j++) { point = 0; b[cnt] = new jbutton(); b[cnt].setbounds(x, y, 20, 20); b[cnt].setbackground(color.white); p1.add(b[cnt]); if (put == 0) { try { string sql = "insert book values('" + movname + "','" + movtime + "','" + movdate + "'," + x + "," + y + ",1)"; string url = "jdbc:odbc:tbs"; connection con = drivermanager.getconnection(url); statement s = con.createstatement(); s.executeupdate(sql); con.close(); } catch (exception ex) { system.out.println(ex); } } else { try { string sql = "select * book"; string url = "jdbc:odbc:tbs"; connection con = drivermanager.getconnection(url); statement s = con.createstatement(); rs = s.executequery(sql); while (rs.next()) { string getname = rs.getstring("movname"); string getdate = rs.getstring("movdate"); string gettime = rs.getstring("movtime"); avail = integer.parseint(rs.getstring("avail")); if (movname.equalsignorecase(getname) && movdate.equals(getdate) && movtime.equals(gettime)) { k++; if (avail == 0 && k == (cnt + 1)) { b[cnt].setbackground(color.red); point = 1; b[cnt].setenabled(false); } } } } catch (exception ex) { system.out.println(ex); } } if (point == 0) { b[cnt].addmouselistener(this); } cnt++; x = x + 30; k = 0; } x = 150; y = y + 40; } y = 300; x = 150; p3 = new jpanel(); p3.setlayout(null); p3.setbackground(color.lightgray); p3.setbounds(100, 260, 500, 20); p1.add(p3); l2 = new jlabel("economy"); l2.setbounds(220, 0, 100, 20); p3.add(l2); l2.setbackground(color.red); (i = 1; <= 2; i++) { (j = 1; j <= 14; j++) { point = 0; b[cnt] = new jbutton(); b[cnt].setbounds(x, y, 20, 20); b[cnt].setbackground(color.white); p1.add(b[cnt]); if (put == 0) { try { string sql = "insert book values('" + movname + "','" + movtime + "','" + movdate + "'," + x + "," + y + ",1)"; string url = "jdbc:odbc:tbs"; connection con = drivermanager.getconnection(url); statement s = con.createstatement(); s.executeupdate(sql); con.close(); } catch (exception ex) { system.out.println(ex); } } else { try { string sql = "select * book"; string url = "jdbc:odbc:tbs"; connection con = drivermanager.getconnection(url); statement s = con.createstatement(); rs = s.executequery(sql); while (rs.next()) { string getname = rs.getstring("movname"); string getdate = rs.getstring("movdate"); string gettime = rs.getstring("movtime"); avail = integer.parseint(rs.getstring("avail")); if (movname.equalsignorecase(getname) && movdate.equals(getdate) && movtime.equals(gettime)) { k++; if (avail == 0 && k == (cnt + 1)) { b[cnt].setbackground(color.red); point = 1; b[cnt].setenabled(false); } } } } catch (exception ex) { system.out.println(ex); } } if (point == 0) { b[cnt].addmouselistener(this); } cnt++; x = x + 30; k = 0; } x = 150; y = y + 40; } image = new imageicon("screen.jpg"); l3 = new jlabel(image); l3.setbounds(70, y + 20, 600, 54); p1.add(l3); l4 = new jlabel("seat occupy"); l4.setbounds(140, 490, 100, 15); p1.add(l4); btnocc = new jbutton(); btnocc.setbounds(260, 490, 15, 15); btnocc.setbackground(color.white); p1.add(btnocc); l4 = new jlabel("booked seat"); l4.setbounds(320, 490, 80, 15); p1.add(l4); btnbook = new jbutton(); btnbook.setbounds(410, 490, 15, 15); btnbook.setbackground(color.red); p1.add(btnbook); l4 = new jlabel("current selection"); l4.setbounds(460, 490, 100, 15); p1.add(l4); btnsel = new jbutton(); btnsel.setbounds(570, 490, 15, 15); btnsel.setbackground(color.green); p1.add(btnsel); setsize(770, 650); setvisible(true); } public void mousepressed(mouseevent e) { } public void mousereleased(mouseevent e) { } public void mouseentered(mouseevent e) { cursor mycursor = new cursor(cursor.hand_cursor); setcursor(mycursor); } public void mouseexited(mouseevent e) { cursor mycursor = new cursor(cursor.default_cursor); setcursor(mycursor); } public void mouseclicked(mouseevent e) { int row, col, ex; (i = 0; < cnt; i++) { if (e.getsource().equals(b[i])) { if (count < quan) { prev[count] = i; b[prev[count]].setbackground(color.green); row = / 14; col = % 14; x1 = 150 + (30 * col); if (row >= 0 && row <= 3) { y1 = 90 + (40 * row); } else { y1 = 300 + (40 * (row - 4)); } pos[count][0] = x1; pos[count][1] = y1; count++; } else { ex = prev[0]; b[ex].setbackground(color.white); (int j = 0; j <= (count - 1); j++) { prev[j] = prev[j + 1]; pos[j][0] = pos[j + 1][0]; pos[j][1] = pos[j + 1][1]; } prev[count - 1] = i; row = / 14; col = % 14; x1 = 150 + (30 * col); if (row >= 0 && row <= 3) { y1 = 90 + (40 * row); } else { y1 = 300 + (40 * (row - 4)); } pos[count - 1][0] = x1; pos[count - 1][1] = y1; (int j = 0; j <= (count - 1); j++) { b[prev[j]].setbackground(color.green); } count = quan; } } } } public void actionperformed(actionevent e) { if (e.getsource() == save) { if (count != quan) { joptionpane.showmessagedialog(null, "please select exact numbers of seat", "error", joptionpane.error_message); } else { (i = 0; < count; i++) { try { string sql = "update book set avail=0 movname='" + movname + "' , movdate='" + movdate + "' , movtime='" + movtime + "' , x=" + pos[i][0] + " , y=" + pos[i][1] + " "; string url = "jdbc:odbc:tbs"; connection con = drivermanager.getconnection(url); statement s = con.createstatement(); s.executeupdate(sql); con.close(); } catch (exception ex) { system.out.println(ex); } setvisible(false); bookmain bm = new bookmain(); } } } } public static void main(string args[]) { bookshow mm = new bookshow(); } }
java progress bar tutorial should help.
another example can found in swingworker java docs entry.
instead of calling nextprimenumber()
in case processnextbook()
.
to work out progress use similar idea bellow changing variables meaningful in code eg. setprogress(100 * booksprocessed.size() / books.size());
the code bellow should give basic idea how achieve looking for.
class primenumberstask extends swingworker<list<integer>, integer> { primenumberstask(jtextarea textarea, int numberstofind) { //initialize } @override public list<integer> doinbackground() { while (! enough && ! iscancelled()) { number = nextprimenumber(); publish(number); setprogress(100 * numbers.size() / numberstofind); } } return numbers; } @override protected void process(list<integer> chunks) { (int number : chunks) { textarea.append(number + "\n"); } } } jtextarea textarea = new jtextarea(); final jprogressbar progressbar = new jprogressbar(0, 100); primenumberstask task = new primenumberstask(textarea, n); task.addpropertychangelistener( new propertychangelistener() { public void propertychange(propertychangeevent evt) { if ("progress".equals(evt.getpropertyname())) { progressbar.setvalue((integer)evt.getnewvalue()); } } });
Comments
Post a Comment