Swing in different 2 threads

D

dimitrik107

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

Thomas Hawtin

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
 
D

dimitrik107

Thomas said:
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?
 
C

Chris Smith

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

Thomas Hawtin

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,078
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top