how to make multiple classes working simultaneously!!
hi,
I am having a problem. I am using a class which contains more SQL queries, which will have some seconds to finish the work. I am having another class which runs a ProgressBar with a thread for showing the process. I want to activate both these classes one by one and stop the ProgressBar class's thread after the SQL class completes its process.
What problem arises is, ProgressBar class starts running with the thread,but shows its InternalFrame only after SQL class process is completed. I want to make them working simultaneously. Can u people help me.
bhuvana.
You are probably doing the SQL in the event-thread (in a actionPerformd or something) that will block the GUI from repainting. You need to do that in an other thread. I suggest you have one thread that does the SQL stuff and updates the ProgressBar.
hi,I am not using the event-thread for the SQL stuff. Instead, i am calling the method for the SQL stuff immediately after the object for SQL class is created. Where I have to use the thread? Please help me.
Can you post the code where you create the SQL object and do the SQL stuff and also the code that creates the ProgressBar and the thread used for it. Use the [code]-tag.
hi,
I am posting my code here. I don't know what is [code]tag. Please do the needful.
/****method of the class createtables2***/
public void createal()
{
try
{
String tab1="create table test1(name varchar(10));
Statement st=con.createStatement();
st.executeUpdate();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
....
...
}
/***** eof of createall method of createtables2 class*****/
As given above, there are more create statements, which will take some seconds to finish the process.
Here is the code for Progress Bar,
/**** ProgressBar class **********/
public class progressBarclass extends JInternalFrame implements Runnable
{
Thread t;
JProgress Bar pbar;
Container cp=getContentPane();
.........
public progressBarclass(String s,boolean b1,boolean b2,boolean b3,boolean b4)
{
super(s,b1,b2,b3,b4);
cp.setLayout(null);
pbar=new JProgressBar();
pbar.setStringPainted(true);
pbar.setBounds(300,100,400,20);
cp.add(pbar);
}
//method for starting the thread
public void threadstart()
{
t=new Thread(this);
t.start();
}
//method for stopping the thread
public void threadstop()
{
try
{
t.stop();
}
catch(Exception e){}
}
//method for running thread
public void run()
{
while(true)
{
try
{
pbar.setValue(pbar.getValue()+1);
Thread.sleep();
}
catch(InterruptedException ie){}
}
}//eof run
/**********eof progressBarclass *********/
I am calling above two classes in a Frame extended class, which is given below.
/********main class *********/
.........
.........
//this object is created in MenuItem's actionPerformed event.
progressBarclass pbarclass=new progressBarclass("PROGRESSBAR",false,true,true,true);
pbarclass.setBounds(0,0,800,400);
desk.removeAll();//JDesktopPane object
pbarclass.threadrun();
desk.add(pbarclass);
//calling the SQL class next to the above statement
createtables2 ctables=new createtables2(arg1);
ctables.createall();
pbarclass.threadstop();
/*******eof actionevent *******/
Hope u can help me.thanks
bhuvana.
hi,
I am posting my code here. I don't know what is tag. Please do the needful.
[code]
/****method of the class createtables2***/
public void createal()
{
try
{
String tab1="create table test1(name varchar(10));
Statement st=con.createStatement();
st.executeUpdate();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
....
...
}
/***** eof of createall method of createtables2 class*****/
As given above, there are more create statements, which will take some seconds to finish the process.
Here is the code for Progress Bar,
/**** ProgressBar class **********/
public class progressBarclass extends JInternalFrame implements Runnable
{
Thread t;
JProgress Bar pbar;
Container cp=getContentPane();
.........
public progressBarclass(String s,boolean b1,boolean b2,boolean b3,boolean b4)
{
super(s,b1,b2,b3,b4);
cp.setLayout(null);
pbar=new JProgressBar();
pbar.setStringPainted(true);
pbar.setBounds(300,100,400,20);
cp.add(pbar);
}
//method for starting the thread
public void threadstart()
{
t=new Thread(this);
t.start();
}
//method for stopping the thread
public void threadstop()
{
try
{
t.stop();
}
catch(Exception e){}
}
//method for running thread
public void run()
{
while(true)
{
try
{
pbar.setValue(pbar.getValue()+1);
Thread.sleep();
}
catch(InterruptedException ie){}
}
}//eof run
/**********eof progressBarclass *********/
I am calling above two classes in a Frame extended class, which is given below.
/********main class *********/
.........
.........
//this object is created in MenuItem's actionPerformed event.
progressBarclass pbarclass=new progressBarclass("PROGRESSBAR",false,true,true,true);
pbarclass.setBounds(0,0,800,400);
desk.removeAll();//JDesktopPane object
pbarclass.threadrun();
desk.add(pbarclass);
//calling the SQL class next to the above statement
createtables2 ctables=new createtables2(arg1);
ctables.createall();
pbarclass.threadstop();
/*******eof actionevent *******/
Hope u can help me.thanks
bhuvana.
hi, Sorry, I could not use the [code] tag well. Please help me.bhuvana.
hi,
I am posting the code again. Please do the needful.
/***method of the class createtables2****/
public void createall()
{
try
{
String tab1="create table test1(name varchar(10));
Statement st=con.createStatement();
st.executeUpdate();
}
catch(Exception e){}
....
.........
}
About how to use the code-tag.
Write like this:
[code]
// put your code here
[/code]
Then the code will be syntax highlighted and indentation is not destroyed.
There is a " missing in your code therefore everything is in blue. No need for you to repost though, I understand the code anyway.
Your problem is that the createal method is called in the event-thread ("this object is created in MenuItem's actionPerformed event.") and that will block that thread.
I would do it this way:
Make the ProgressBar class not a thread.
Make the createtables2 class a thread instead.
Add a method to the PrograaBar class to increase its value.
Make the createal method call that method between each sql-statement.
/**** ProgressBar class **********/
public class MyProgressBar extends JInternalFrame {
JProgressBar pbar;
Container cp = getContentPane();
.........
public MyProgressBar (String s,boolean b1,boolean b2,boolean b3,boolean b4) {
super(s,b1,b2,b3,b4);
cp.setLayout(null);
pbar=new JProgressBar();
pbar.setStringPainted(true);
pbar.setBounds(300,100,400,20);
cp.add(pbar);
}
public void setMax(int max) {
pbar.setMaximum(max);
}
public void increaseValue() {
pbar.setValue(pbar.getValue()+1);
}
}
/**********eof MyProgressBar class *********/
/****class createtables2***/
public class CreateTable2 impelements Runnable {
private static final int NUM_TABLES = 2; // Set to the number of calls to increaseValue
private Thread tread;
private MyPrograaBar bar;
publie CreateTable2(int arg1, MyProgressBar b) {
tread = new Thread(this);
bar = b;
bar.setMax (NUM_TABLES);
}
public void startThread() {
thread.start();
}
public void run() {
createal();
}
public void createal() {
try {
String tab1="create table test1(name varchar(10))";
Statement st=con.createStatement(tab1);
st.executeUpdate();
} catch(Exception e) {
System.out.println(e.getMessage());
}
bar.increaseValue();
try {
String tab2="create table test2(name varchar(10))";
Statement st=con.createStatement(tab2);
st.executeUpdate();
} catch(Exception e) {
System.out.println(e.getMessage());
}
bar.increaseValue();
....
}
/***** eof of createtables2 class*****/
/********main class *********/
..................
//this object is created in MenuItem's actionPerformed event.
progressBarclass pbarclass=new progressBarclass("PROGRESSBAR",false,true,true,true);
pbarclass.setBounds(0,0,800,400);
desk.removeAll(); //JDesktopPane object
desk.add(pbarclass); //calling the SQL class next to the above statement
createtables2 ctables=new createtables2(arg1, pbarclass);
ctables.startThread();
/*******eof actionevent *******/
You may need to do some changes to the code but I hope you get the general idea behind it, if not just ask.
hi Daniel,The code is working fine. Thank u so much for ur time and effort.bhuvana.