I'm currently working on displaying the source code. I have a JTextArea in
a scroll pane. When the instrumented source begins executing a copy of the
source file is pass to the JTextArea where, line by line it is appended.
The problem is I don't actually see the source displayed until the
instrumented program has finished executing completely. I know the method
is 'working'as each time I add a line to the textArea I print it to the
console and that works.
Does anybody know if there is an equivalent to repaint() or refresh for a
text component or someway to force the display immediately?
Sharon
The core problem is probably that you are performing lengthy operations
from the Event Dispatch Thread, which is a no-no because it will block
any UI update calls until the lengthy operation is complete, resulting
in the behavior you describe. The solution is pretty simple. You run
the lengthy operation on a separate thread. Here's a simplified
example:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class TestFrame extends JFrame {
private JTextArea textArea;
public TestFrame() {
setTitle("Responsive UI");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
getContentPane().setLayout(new BorderLayout());
final JButton countButton = new JButton("Count");
countButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Thread t = new Thread(new Runnable() {
public void run() {
countButton.setEnabled(false);
doLongOperation();
countButton.setEnabled(true);
}
});
t.start();
}
});
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(countButton, BorderLayout.SOUTH);
setSize(300, 300);
setVisible(true);
}
void doLongOperation() {
for (int i = 0; i < 1000; i++) {
textArea.append(i + "\n");
}
}
public static void main(String[] args) {
new TestFrame();
}
}
Jim S.
> Sharon
Jim Sculley <nic...@abraxis.com> wrote in message
news:3A84341E...@abraxis.com...
Note that access outside the class and monitor ownership are mostly
separate issues. Providing for access by other classes is simply a
matter of creating public methods to do so. Proper use of wait() and
notify() is more complex.
It is a requirement that the calling thread own the monitor of the
object whose wait() or notify() method is being called. The Javadocs
for notify() describe how a thread can get ownership of an object's
monitor:
========
A thread becomes the owner of the object's monitor in one of three ways:
- By executing a synchronized instance method of that object.
- By executing the body of a <code>synchronized</code> statement
that synchronizes on the object.
- For objects of type <code>Class,</code> by executing a
synchronized static method of that class.
========
Can you provide a little more information about what you wish to
accomplish using wait() and notify()?
Jim S.
Basically it annotates everyline in the source file with a method call to
another class passing in details of position in the instrumented program.
This method then decides if and what should be displayed. The decision on
whether to update the display depends on whether the user has decided to
step into/over/out of the currently executing method (For example if the
user has stepped over the instrumented source should continue executing
UNTIL the line executing is in the same class and method previously
displayed, then the display should be updated highlighting the next line.
If execution calls other objects/classes/methods then nothing in the display
should change and execution continues.
What I want to do is begin execution of the instrumented file, which is
currently occuring on my new thread. I want to display the source in my GUI
and highlight the currently executing line. At this point the instrumented
source should be waiting(). The user then decides to step into/over/out and
this notifies() the instrumented source to execute until the display should
be updated again. At which point it should wait()..... and so on....
I can't instrument every line in the source with thread.wait() as each time
I want to execute 1...n statements.
hope this explains a little more..... am continuing reading
thanks
Jim Sculley <nic...@abraxis.com> wrote in message
news:3A8A9628...@abraxis.com...
In article <qOui6.5816$hN2.1...@news3.cableinet.net>, on Wed, 14 Feb 2001 12:24:54 GMT, "Sharon
Tipper" <ano...@home.com> wrote:
javax.swing.JComponent.paintImmediately()