Re: swing design questions
conrad wrote:
public class ButtonDemo extends JFrame {
public class CheckBoxDemo extends ButtonDemo {
Two things going on here. First look carefully at those declarations.
CheckBoxDemo is a kind of JFrame, not a kind of checkbox. It's a demo
for checkboxes, not a demo that is-a checkbox. It has-a checkbox.
Second, your book is old or sloppily written. All those objects must be
created on the EDT. See Swing's threading policy.
<http://java.sun.com/javase/6/docs/api/javax/swing/package-summary.html#threading>
Note that they use SwingUtilities.invokeLater to create all JFrames and
Swing objects. You must do this for all Swing methods that are not
listed as thread safe. Yes, it's a pain.
SwingUtilities.invokeAndWait is also available and sometimes better,
because it's synchronized with the execution of the calling thread.
OK, now that those two things are out of the way, I'll try to answer
your question a bit more. First, extending JFrame like your book shows
is common and perfectly ok. Extending a base object which extends
JFrame like CheckBoxDemo does... well I haven't seen that as much. But
if there's common functionality that all of your windows share, then it
does make sense. In this case, I think the author is basically saving
himself some typing (typing as in wear and tear on the fingertips, not
typing as in a Java type). But it works, and that's ok.
You might try to write some simple applications to test out your ideas.
Making your own applications is a good way to make sure you've got
everything straight in your head. I'd suggest something like a simple
Fahrenheit to Celsius conversion to begin. Dead simple in concept, but
a little tricky with Swing.