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 / September 2006

Tip: Looking for answers? Try searching our database.

Swing in different 2 threads

Thread view: 
dimitrik107@hotmail.com - 15 Sep 2006 15:06 GMT
I had interview question. She asked me if you have 2 threads and you
have class extending from the JFrame you can safely display class from
each thread or not and why? I do not know answer. I know recomendation
is to work with Swing from only main thread. Can someone explain me
answer to such question? Thank you for this help.
Thomas Hawtin - 15 Sep 2006 15:29 GMT
> I had interview question. She asked me if you have 2 threads and you
> have class extending from the JFrame you can safely display class from
> each thread or not and why? I do not know answer. I know recomendation
> is to work with Swing from only main thread. Can someone explain me
> answer to such question? Thank you for this help.

If you've got a class extending JFrame, you are probably extending
classes inappropriately. But apparently bad coding is fine for GUI
applications...

Unless stated otherwise, all Swing components should be accessed from
the AWT Event Dispatch Thread (EDT). They are not thread-safe (in fact
they are thread-hostile).

Note the EDT is not the main thread.

To be really pedantic:

The subclass of JFrame could add methods that are thread-safe. Perhaps a
method that calls setVisible from the correct thread (using
EventQueue.invokeLater/invokeAndWait).

In applets and JNLP/WebStart you will have more than one EDT in the same
JVM instance. As you said class, you could have two instances of the
same class on different EDTs (class loaders permitting).

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

dimitrik107@hotmail.com - 15 Sep 2006 16:17 GMT
> If you've got a class extending JFrame, you are probably extending
> classes inappropriately. But apparently bad coding is fine for GUI
> applications...

Thank you for answer it is more clear now. I don't understand this
point about extendin classed inappropriately? Most sample code for
Swing tutorial has class MyFrame extends JFrame {...} Are you saying
the tutorial code are probably extending classes inappropriately?
Chris Smith - 15 Sep 2006 16:45 GMT
> Thank you for answer it is more clear now. I don't understand this
> point about extendin classed inappropriately? Most sample code for
> Swing tutorial has class MyFrame extends JFrame {...} Are you saying
> the tutorial code are probably extending classes inappropriately?

Yes, that's what Thomas is saying.  He's right, too.

Signature

Chris Smith

Thomas Hawtin - 15 Sep 2006 16:56 GMT
>> If you've got a class extending JFrame, you are probably extending
>> classes inappropriately. But apparently bad coding is fine for GUI
[quoted text clipped - 4 lines]
> Swing tutorial has class MyFrame extends JFrame {...} Are you saying
> the tutorial code are probably extending classes inappropriately?

Yes.

See Effective Java, Item 14: Favor composition over inheritance.

Indeed almost all of the code should not be dependent on being in a
JFrame. There's two main motivations for this: Firstly, you want to keep
your code as simple as possible, so don't involve inheritance. A
secondary point is that you may want to move your JFrame contents to,
say, a JInternalFrame, a JDialog, a JSplitPane, a JApplet, a JFrame
subclassed for some genuine purpose, another layer of panels between
frame and your contents, or something you haven't thought of yet.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



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.