Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JTextArea - immediate display?

453 views
Skip to first unread message

Sharon Tipper

unread,
Feb 9, 2001, 12:22:28 PM2/9/01
to
I am writing an OO visualiser which works in the same way as a debugger -
instrument the source then run the instrumented source with instructions for
display.

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


Jim Sculley

unread,
Feb 9, 2001, 1:17:02 PM2/9/01
to

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

Sharon Tipper

unread,
Feb 14, 2001, 7:24:54 AM2/14/01
to
this works, however how can I contol this new thread with wait() and
notify() calls.... I just get current thread not owner exceptions..... I
made it a class variable but I want to access it from both within and
outside the class in which it was created?


Jim Sculley <nic...@abraxis.com> wrote in message
news:3A84341E...@abraxis.com...

Jim Sculley

unread,
Feb 14, 2001, 9:28:56 AM2/14/01
to
Sharon Tipper wrote:
>
> this works, however how can I contol this new thread with wait() and
> notify() calls.... I just get current thread not owner exceptions..... I
> made it a class variable but I want to access it from both within and
> outside the class in which it was created?

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.

Sharon Tipper

unread,
Feb 14, 2001, 12:04:49 PM2/14/01
to
I am writing an OO visualiser. This visualiser works in a similar way to a
debugger: it reads in and parses the source files to be 'visualised' and
then annotates these sources with instructions which will be invoked by the
visualiser to produce the display. The Instrumenter was written by a tutor
at University so I'm pretty limited in changes I can make to it.

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...

David Postill

unread,
Feb 27, 2001, 5:46:34 PM2/27/01
to
[This followup was posted to comp.lang.java.gui, cc: the cited author, Sharon Tipper]

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()

0 new messages