I have got a form in Java (Swing) loading large amount of data from the database. I want to display a progress bar while the program gets actually loaded.
How can i do it?
The code is as 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 * from 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 from 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 * from 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);
}
for (i = 1; i <= 4; i++) {
for (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 into 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 * from 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);
for (i = 1; i <= 2; i++) {
for (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 into 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 * from 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 to 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;
for (i = 0; i < cnt; i++) {
if (e.getSource().equals(b[i])) {
if (count < quan) {
prev[count] = i;
b[prev[count]].setBackground(Color.green);
row = i / 14;
col = i % 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);
for (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 = i / 14;
col = i % 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;
for (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 the exact numbers of seat", "Error", JOptionPane.ERROR_MESSAGE);
} else {
for (i = 0; i < count; i++) {
try {
String sql = "Update Book set Avail=0 where MovName='" + movname + "' AND MovDate='" + movdate + "' AND MovTime='" + movtime + "' AND X=" + pos[i][0] + " AND 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();
}
}