Hmmm... I checked out the first few pages of that sight, but I had to flee in
horor when they suggested overriding JFrame's paint method. Aiieee!!!
Sun's Swing tutorial is very good. Here is the HelloWorld page: http://java.sun.com/docs/books/tutorial/uiswing/learn/example1.html
And here is the overall starting point: http://java.sun.com/docs/books/tutorial/uiswing/index.html
> (Don't extend JFrame)
What about the following?
public class SomeFrame extends JFrame {
private JButton someButton;
private JTextArea someTextArea;
...
}
I'm playing Devil's advocate because I try to subclass only when I *have* to. But I wish I had a nickel for ever time I say this style of coding for frames and panels.
>What about the following?
You add() the components to a frame. Well, at least it has private modifiers. I would, nevertheless, stick to using a JFrame as it is, as long as it provides all the functionality I need (and it has so far).
What I find most horribliest is this
public class TheFrame extends JFrame implements <insert list of listeners here>
{
//...
}
Talk about bloating a class.
#
Similarly,
public class ThePanel extends JPanel implements <insert list of listeners here>
{
//...
}
There are any number of things wrong with having a frame/panel subclass defined this way.
1. Often coders with any number of disparate buttons make their
frame or panel listener to them all:
public class ThePanel extends JPanel implements ActionListener
Then the actionPerformed method is a laundry list of if/then/elseifs. Ugh.
That's the perfect situation for specific action objects listening to specific controls.
2. Exposing implementation details: the fact that the panel is listening
to list selection is not part of the API for the panel, it's an implementation
detail, yet that's not what this reads as:
public class ThePanel extends JPanel implements ListSelectionListener
What's to stop other code from doing the following?
unrelatedList.addListSelectionListener(thePanel); //woops, thePanel wasn't meant to listen to *that* list.
It's surprising what cruft people won't tolerate in other areas of code are
happy to commit in Swing code.
Message was edited by:
DrLaszloJamf