By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,032 Members | 825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,032 IT Pros & Developers. It's quick & easy.

Updating GUI While Busy -- Using Thread, but Can't Access Variables

P: n/a
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread
to create a new thread.**These*are*part*of*the*problem.

I want to have something running in the background, while the GUI is
updating.**I've*done*that*before*without*a*problem ,*however,*now,*I*need*to
pass variables to the separate Thread or Runnable that I'm using.**I'm
using something like this:

//Other code setting things up and updating GUI
//Variables defined out here (can't finalize, though)
Thread timer = new Thread() {
********public*void*run()*{
****************//code*here*to*run*WHILE*other*code*updates*the*GUI
//Want to access variables here
********}
};
timer.start();
//More code updating the GUI

I have yet to find a way to pass variables to the inner thread unless
they're finalized.**How*can*I*let*the*inner*Thread*(I'm*cr eating*it*as*a
class within a class) have access to the variables in the outer class?

Thanks!

Hal
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hal Vaughan wrote:
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread
to create a new thread. These are part of the problem.

I want to have something running in the background, while the GUI is
updating. I've done that before without a problem, however, now, I need to
pass variables to the separate Thread or Runnable that I'm using. I'm
using something like this:

//Other code setting things up and updating GUI
//Variables defined out here (can't finalize, though)
Thread timer = new Thread() {
public void run() {
//code here to run WHILE other code updates the GUI
//Want to access variables here
}
};
timer.start();
//More code updating the GUI

I have yet to find a way to pass variables to the inner thread unless
they're finalized. How can I let the inner Thread (I'm creating it as a
class within a class) have access to the variables in the outer class?


Hal,

What kind of variables are you talking about? If they are instance
variables, you can access them. If they are local variables, what is
the point of updating them, they are (most likely) gone by the time your
thread runs.

Perhaps some example code would better explain your situation.

Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2

P: n/a
Raymond DeCampo wrote:
Hal Vaughan wrote:
First, I am aware of both SwingUtilities.invokeLater(), and of using
Thread
to create a new thread. These are part of the problem.

I want to have something running in the background, while the GUI is
updating. I've done that before without a problem, however, now, I need
to
pass variables to the separate Thread or Runnable that I'm using. I'm
using something like this:

//Other code setting things up and updating GUI
//Variables defined out here (can't finalize, though)
Thread timer = new Thread() {
public void run() {
//code here to run WHILE other code updates the GUI
//Want to access variables here
}
};
timer.start();
//More code updating the GUI

I have yet to find a way to pass variables to the inner thread unless
they're finalized. How can I let the inner Thread (I'm creating it as a
class within a class) have access to the variables in the outer class?


Hal,

What kind of variables are you talking about? If they are instance
variables, you can access them. If they are local variables, what is
the point of updating them, they are (most likely) gone by the time your
thread runs.

Perhaps some example code would better explain your situation.

Ray


I have a number of string variables I want to access from the inner thread
(things like the log file name), and I want the inner thread to update a
string that is (sometimes) displayed in a JTextArea in the GUI.

Right now I'm experimenting with using static variables in the original
(outer) class, as per a note I saw in a Google search. It's not what I'd
like to do, but it looks like it may work. I've tried to access regular
string variables created in the outer class, but I've gotten errors that
say they need to be finalized.

Right now I've got the code in the middle of changing it to test using
static variables (I goofed and deleted the backup -- it hapens when I work
all night!), so posting code would be a nightmare. I can probably do it
when I've got something that checks out (but that still won't update the
GUI).

Thanks!

Hal
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.