Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello! I'm trying to do some work in java, but am having some serious difficulties. This is the problem, I have my main program in a class, say class A. This class creates a thread, class B. This thread does nothing more than update a variable. My problem is that the variable needs to be read by class A, which needs to display that variable in a JTextField. I would post source code, but its fairly large and ugly :P so here is a uqick extract: (any ideas?) (really this is just a counter example, but I have no idea how to sync the two instances of my "counter", i.e. the one in class A that can display it, and the one in class B that updates it)
[code]
class A
{
static Mat mat;
mat.init();
Thread matTransmit = new MatSender (mat);
matTransmit.start();
JTextField myText;
myText.setText(mat.stateList);
}public class MatSender extends Thread{
private Mat mat;
MatSender(Mat m){
this.mat =m;
}
public void run(){
while (true){
/*
* Non critical section
*/
cs();
}
}
private void cs(){
// critical section
String matData = mat.stateList();
matBuffer.setText(matData);
}
}package virtua;
public class Mat {
boolean[][] mat = new boolean[9][9];
void init (){
for (int i=0; i<9; i++){
for (int j=0; j<9; j++){
mat[i][j] = false;
}
}
}
void pressAt(int x, int y){
mat[x][y] = true;
}
void releaseAt(int x, int y){
mat[x][y] = false;
}
boolean getState(int x, int y){
return mat[x][y];
}
String stateList(){
String matState = "{";
for (int i=0; i<9; i++){
for (int j=0; j<9; j++){
if (mat[i][j])
{
matState += "(" +(i+1)+ ", " +(j+1)+ ")";
}
}
}
matState += "}";
return matState;
}
}
[/code]

Just realised, I didn't explain myself correctly! Simplified:
I have 2 classes, one which creates a GUI and one with needs to update a single element within that GUI every x seconds.Im implementing this as a thread, so the GUI is created, and a thread is created to the updateGUI class. This now runs on its own while the user does his business, and updates the JTextField every x seconds.
Ideas?

I think I got it! Thru random experimentation, this seems to work:
A.matBuffer.setText(mat.stateList());(where A is my main class)
Just thought i'de post this for posterity :P

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |