-
March 16th, 2011, 12:58 PM
#1
Getting Data From a class without making an instance of the class
Hello everyone,
I was wondering if this process was possible as making an instance of a class that has GUI properieties creates another window when I make the instance of that class.
What I am trying to do is simulating people arriving at a till and leaving it.
I have a
-SuperMarketGUI class which is the class that has all the GUI proprieties value(such as frame,JTextArea,JButton etc)
-ThreadStarted class which is called when the start Button on the SuperMarketClass is pressed.
-Checkout class which process the simulation data
Code:
public class ThreadStarter extends Thread {
public ThreadStarter()
{
}
Checkout chk=new Checkout();
public void run()
{
try {
for(int t=2;t<=10;t++)
{
chk.simulation(1,t);
sleep((int)(Math.random()*1000));
}
} catch (Exception e)
{
System.err.println(e);
e.printStackTrace();
}
}
public void finish()
{
}
}
The code above runs in a for loop once the start button is pressed from another class; it calls the simulation method which is in the Checkout class.
Now I have this simulation method that generates some data(some integers and some strings).
I want that data to be displayed in the JTextArea of my SuperMarketGUI everytime the simulation method is called.
Code:
public String simulation(int i,int t)
{
//for (int t=2; t<=10; t++) //number of time-steps in the simulation.
//{
rndNumber =(int) (Math.random()*6+1); // generates random number between 1 and 6.
if (rndNumber==4)
{
// if rndNumber is 4, t is added to a queue.
tillQueue.add(String.valueOf(t));
currentLen++;
}
else if ((rndNumber==1 || rndNumber==3) && tillQueue.peek()!=null)
{
tillQueue.remove();
currentLen--;
}
if(tillQueue.isEmpty())
{
date=dt.getDate();
time=dt.getTime();
System.out.println(date+"\t"+time+"\t" +t + "\t" + rndNumber + "\t" + "Queue is empty");
//smGUI.setData(date, time, String.valueOf(rndNumber), "Queue Is Empty", i);
lengthList.add(new Integer(currentLen)); //using wrapper
return ("\n"+date+"\t"+time+"\t"+rndNumber+"\t"+"Queue Is Empty");
}
else
{
System.out.println(date+"\t"+time+"\t"+t + "\t" + rndNumber+"\t" + tillQueue.toString() );
//smGUI.setData(date, time, String.valueOf(rndNumber), tillQueue.toString(), i);
smGUI.info[i].append("\n"+date +"\t"+time+"\t"+ String.valueOf(rndNumber)+"\t"+ tillQueue.toString());
lengthList.add(new Integer(currentLen)); //using wrapper
return ("\n"+date+"\t"+time+"\t"+rndNumber+"\t"+tillQueue);
}
}
How can I do that?
Thank you
-
March 16th, 2011, 04:43 PM
#2
Re: Getting Data From a class without making an instance of the class
If you want have data from a thread displayed in a GUI component, use SwingWorker instead of a plain thread. It's a thread enhanced to communicate with the Swing event dispatch thread without jamming it. Check out the examples in the API docs, and the tutorial here.
Today, most software exists, not to solve a problem, but to interface with other software...
I. O. Angell
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
March 17th, 2011, 12:23 PM
#3
Re: Getting Data From a class without making an instance of the class
Thank you for the links, they are really useful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|