Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Question: Simple GUI Updater Thread

Thread view: 
pek - 30 Oct 2007 12:21 GMT
Hello.

Is this a good way to write an updater thread:

Executors.newScheduledThreadPool(1).schedule(new Runnable() {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
testLabel.setText("Test");

}
});
}
},1,TimeUnit.SECONDS);
}

It looks very ugly.. :P
It works perfectly nonetheless.
Leonard Milcin - 30 Oct 2007 13:57 GMT
> Hello.
>
[quoted text clipped - 14 lines]
> It looks very ugly.. :P
> It works perfectly nonetheless.

Yes it will work. No, it's not correct.

Google for SwingWorker (bundled with jdk since 1.6). You can also use
EventQueue instead of SwingUtilities. invokeLater() and invokeAndWait()
just forward to same methods in EventQueue.

Regards,
Leonard

Signature

It is wrong always, everywhere, and for anyone, to believe anything
upon insufficient evidence.
                                        -- William Kingdon Clifford

Lew - 30 Oct 2007 14:27 GMT
> Executors.newScheduledThreadPool(1).schedule(new Runnable() {
> public void run() {
[quoted text clipped - 9 lines]
>
> It looks very ugly.. :P

It would look prettier with proper indentation.

Signature

Lew

Roedy Green - 30 Oct 2007 15:08 GMT
>Is this a good way to write an updater thread:

See http://mindprod.com/jgloss/timer.html

Swing has a Timer specially for the purpose.  It would be more
idiomatic and terser.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Mark Space - 30 Oct 2007 19:40 GMT
> Hello.
>
[quoted text clipped - 14 lines]
> It looks very ugly.. :P
> It works perfectly nonetheless.

Yeah, it does.  I think you might break things up a bit just for
readability.

Executors.newShceduledTreadPool(1).schedule( new MyLabelSetter (), 1,
TimeUnit.SECONDS );

class MyLabelSetter implements Runnable {
    public void run() {
        SwingUtilities.invokeLater( new MySetLabel() );
    }
}

class MySetLabel implements Runnable {
    public void run() {
        test.Label.setText( "Test" );
    }
}

(Note: not compiled.)

It's more classes, but I can also read the darn thing.

On the other hand, it's not really that much work to parse through your
example, even with the lack of indentation.  It's kind of a guess how
hard of a time the guy/gal after you is going to have understanding or
modifying your code.

Summary: *shrug*
Lew - 31 Oct 2007 00:38 GMT
> On the other hand, it's not really that much work to parse through your
> example, even with the lack of indentation.  It's kind of a guess how
> hard of a time the guy/gal after you is going to have understanding or
> modifying your code.
>
> Summary: *shrug*

There is an expectation that a Java programmer understands anonymous class
idioms.  They are only confusing at first.  If the programmer after the OP
cannot grok anonymous class declarations, they need to change jobs, perhaps to
one involving offering "fries with that".

Signature

Lew

pek - 31 Oct 2007 18:53 GMT
> > On the other hand, it's not really that much work to parse through your
> > example, even with the lack of indentation.  It's kind of a guess how
[quoted text clipped - 10 lines]
> --
> Lew

First of all, the indention is missing there because many people told
me not to indent code to google groups (I still have no clue why). I
know it looks better with indention (it is intended in my code).

Secondly, I know what an anonymous class is and etc. It's not that I
don't know what they are and how to use them (i didn't copy & paste
the code). It's that I don't really think it's a pretty thing creating
a thread that creates another thread to update a GUI.

Thank you Milcin for the SwingWorker and EventQueue tips. I have read
about the SwingWorker but didn't know if this is right for my job.
I'll have a look at it.

Thank you everybody for the help.

regards
- pek
Lew - 01 Nov 2007 00:20 GMT
> Secondly, I know what an anonymous class is and etc.

I was addressing Mark Space's comment.

I don't know why the person who told you to avoid indentation on Usenet would
mislead you so horridly.

You can indent less on Usenet to be kind to newsreaders, for example use 2
spaces instead of your normal indent level, but it is an invaluable aid to
readability and consistent with the folk standard to indent.

Signature

Lew

John W. Kennedy - 01 Nov 2007 00:49 GMT
> I don't know why the person who told you to avoid indentation on Usenet
> would mislead you so horridly.

Probably said "tabs" originally.
Signature

John W. Kennedy
"The poor have sometimes objected to being governed badly; the rich have
always objected to being governed at all."
  -- G. K. Chesterton.  "The Man Who Was Thursday"

Andrew Thompson - 01 Nov 2007 03:33 GMT
...
>I don't know why the person who told you to avoid indentation on Usenet would
>mislead you so horridly.

Nor do I.  But then, I suspect the advice was more along
the lines of 'post short lines' and 'do not include tabs'.
'remove all indentation' is not (generally) part of the advice.
..
>You can indent less on Usenet to be kind to newsreaders, for example use 2
>spaces instead of your normal indent level, but it is an invaluable aid to
>readability and consistent with the folk standard to indent.

Here is a tool that helps check line width, and replace tabs.
<http://www.physci.org/twc.jnlp>

Signature

Andrew Thompson
http://www.athompson.info/andrew/

pek - 01 Nov 2007 22:51 GMT
> ..
>
[quoted text clipped - 12 lines]
> Here is a tool that helps check line width, and replace tabs.
> <http://www.physci.org/twc.jnlp>
AHhahahahhahaha..
That's a pretty cute tool :P Loved it! ;)

> --
> Andrew Thompsonhttp://www.athompson.info/andrew/
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-general/200711/1
Daniel Pitts - 02 Nov 2007 02:21 GMT
>> Secondly, I know what an anonymous class is and etc.
>
[quoted text clipped - 6 lines]
> 2 spaces instead of your normal indent level, but it is an invaluable
> aid to readability and consistent with the folk standard to indent.

Specifically, make sure your code IS styled, fits within 80 characters
width, and doesn't have *any* tab characters.

As Lew said, you should use 2 spaces for indent, depending on the
nesting level.  I often use 3 or 4, but my design style tends to prevent
 deeply nested scopes that require that much indentation.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Roedy Green - 02 Nov 2007 02:26 GMT
>First of all, the indention is missing there because many people told
>me not to indent code to google groups

They probably told you not to use TAB characters. Indenting is
necessary.

Further, this is a global newsgroup.  Google is one of hundreds of
ways of accessing it.  Google it no way owns it or controls it.

See http://mindprod.com/jgloss/newsgroups.html

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.