From: Sal on
On Jul 12, 5:16 pm, Jeff Higgins <oohigg...(a)yahoo.com> wrote:
> On 7/12/2010 6:41 PM, Sal wrote:
>
>
>
> > On Jul 12, 3:25 pm, Joshua Cranmer<Pidgeo...(a)verizon.invalid>  wrote:
> >> On 07/12/2010 05:41 PM, Sal wrote:
>
> >>> In the following test applet, I would like to have the "Begin counting
> >>> " display immediately, and the "End counting" to display when
> >>> finished, instead the applet waits until the end of the counting loop
> >>> and then displays the text all at once. How can I get it to work
> >>> properly?
>
> >> Answer the following questions:
> >> 1. What are the methods called over the lifecycle of an applet, and in
> >> what order?
> >> 2. On what thread or threads are these methods called?
> >> 3. When is the content of an applet actually displayed?
>
> >> If you can correctly answer all three questions, you should realize why
> >> the text displays all at once. Moving some of the code elsewhere will
> >> ameliorate this problem; which code and where is left as an exercise to
> >> the reader.
>
> > Thanks for your reply. You are very lucky as it's obvious you were
> > never a beginner.
>
> Many of the more experienced readers of this group began at the
> beginning and worked through the basics by reading the documentation and
> writing code and experimenting. A beginner following this path will
> quickly become confident in their knowledge of the basics. See my other
> post for pointers toward the beginning.

Thanks Jeff, but all I have to say was that learning C++ was a helluva
lot easier. And don't try to tell me that Java is more complicated.
BTW, I was always to get straightforward answers on comp.lang.c++.
From: Jeff Higgins on
On 7/12/2010 8:30 PM, Sal wrote:
> On Jul 12, 5:16 pm, Jeff Higgins<oohigg...(a)yahoo.com> wrote:
>> On 7/12/2010 6:41 PM, Sal wrote:
>>
>>
>>
>>> On Jul 12, 3:25 pm, Joshua Cranmer<Pidgeo...(a)verizon.invalid> wrote:
>>>> On 07/12/2010 05:41 PM, Sal wrote:
>>
>>>>> In the following test applet, I would like to have the "Begin counting
>>>>> " display immediately, and the "End counting" to display when
>>>>> finished, instead the applet waits until the end of the counting loop
>>>>> and then displays the text all at once. How can I get it to work
>>>>> properly?
>>
>>>> Answer the following questions:
>>>> 1. What are the methods called over the lifecycle of an applet, and in
>>>> what order?
>>>> 2. On what thread or threads are these methods called?
>>>> 3. When is the content of an applet actually displayed?
>>
>>>> If you can correctly answer all three questions, you should realize why
>>>> the text displays all at once. Moving some of the code elsewhere will
>>>> ameliorate this problem; which code and where is left as an exercise to
>>>> the reader.
>>
>>> Thanks for your reply. You are very lucky as it's obvious you were
>>> never a beginner.
>>
>> Many of the more experienced readers of this group began at the
>> beginning and worked through the basics by reading the documentation and
>> writing code and experimenting. A beginner following this path will
>> quickly become confident in their knowledge of the basics. See my other
>> post for pointers toward the beginning.
>
> Thanks Jeff, but all I have to say was that learning C++ was a helluva
> lot easier. And don't try to tell me that Java is more complicated.
> BTW, I was always to get straightforward answers on comp.lang.c++.

Huh. It's been a long time, but I seem to remember getting a lot of RTFM
on the C/C++ groups. I bought the FMs; 0-470-84573-2, and 0-470-84674-7,
spent months reading them and moved to Java. :-)

I still have them, in good condition, if you know someone who would like
to purchase them.



From: markspace on
Sal wrote:

> public void init() {
> Container cp = getContentPane();
> cp.setBackground( darkGreen );
> cp.setLayout( new FlowLayout() );
> textArea = new JTextArea(30, 40);
> scrollPane = new JScrollPane();
> cp.add ( new JScrollPane( textArea ) );
> textArea.setEditable( false );
> textArea.append( "Begin counting " + nbrOfTimes +" \n" ); //
> <------ look here
> int ctr = 0;
> while( ++ctr < nbrOfTimes );
> textArea.append( "End counting " + ctr +" \n" );


Swing code like that above must be executed on the EDT, or it ain't
going to work. Building on what Jeff Higgins wrote, here's a direct
link to an example which builds a very simple GUI on the EDT:

<http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/deployment/applet/getStarted.html>

You should study that, the rest of the section there on "Applets", and
read through the Concurrency in Swing tutorial:

<http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/concurrency/index.html>

It's a pain in the rear but there isn't any other way. You're totally
stuck until you understand this stuff; we can't write ALL your code for
you. Once you get through with that, here's what I think a basic plan
would be:

1. Construct the first part of your GUI on the EDT as shown in the
example, up to where you append "Begin Counting" to the text area.

2. Find the "long task" part of your GUI; in your example it's that
while loop. That needs to be done OFF the GUI thread or it's going to
block all updates, including displaying the part of the GUI you just
made in #1.

3. Then, when #2 is done, display your final update(s) on the EDT.

Use a SwingWorker for the long task, it's the easiest way. It'll
execute long tasks "in the background" more or less automatically for
you, then run a foreground process on the EDT when it's done. Something
like:

import javax.swing.JTextArea;
import javax.swing.SwingWorker;

public class MyWorker extends SwingWorker {
JTextArea textArea;
int ctr;
@Override
protected Object doInBackground()
throws Exception
{
// your long task here
while( ++ctr < 1000000 );
return null;
}

@Override
protected void done() {
textArea.append( "End counting " + ctr +" \n" );
}
}

That should give you enough to get something done on your own, although
you've got a fair amount of reading to do. Once you get that done and
write the improved version of you code, let us know how you are faring.

(I realize JTextArea.append() is thread safe, just in general you want
to know how to execute code on the EDT after completing some backgound
task.)
From: Arne Vajhøj on
On 12-07-2010 20:30, Sal wrote:
> Thanks Jeff, but all I have to say was that learning C++ was a helluva
> lot easier. And don't try to tell me that Java is more complicated.
> BTW, I was always to get straightforward answers on comp.lang.c++.

In most cases you will also get straightforward answers here, but
sometimes someone conclude that you will learn more by a more
challenging answer.

I think the best way to handle that is to learn from it!

Arne

From: Arne Vajhøj on
On 12-07-2010 20:55, Jeff Higgins wrote:
> On 7/12/2010 8:30 PM, Sal wrote:
>> Thanks Jeff, but all I have to say was that learning C++ was a helluva
>> lot easier. And don't try to tell me that Java is more complicated.
>> BTW, I was always to get straightforward answers on comp.lang.c++.
>
> Huh. It's been a long time, but I seem to remember getting a lot of RTFM
> on the C/C++ groups. I bought the FMs; 0-470-84573-2, and 0-470-84674-7,
> spent months reading them and moved to Java. :-)
>
> I still have them, in good condition, if you know someone who would like
> to purchase them.

I don't know about c.l.c++ but c.l.c is not newbie friendly (or at
least was not 15 years ago).

Arne