When developing Java Swing GUIs, is it always a bad idea to extend JFrame? And what about JPanel, or other JComponents? Also, what makes it bad?
Asked
Active
Viewed 531 times
3
-
1"what makes it bad?" Why do you think it's bad? – Kirk Woll Mar 16 '12 at 17:46
-
1Who said it's a bad idea? As far as I can tell, `JPanel` is *expected* to be extended from for custom components. – Maxpm Mar 16 '12 at 17:47
-
What about JFrame? I honestly don't know what makes it bad. People just make claims that it's bad without giving any reasoning behind it. I'm curious as to what's actually wrong with it. – Bob Mar 16 '12 at 17:48
-
2@Bob Who are those people? I've never heard that. – Maxpm Mar 16 '12 at 17:49
-
@mKorbel I am very curious to know why it is a bad idea – prajeesh kumar Mar 16 '12 at 17:58
-
2@Maxpm JPanel (worse JFrame) are extended way more often than they should be, almost always for the false reasons.. because how often do you really create a new general swing component for your application? If it doesn't make sense to use your newly created `NewFrame` in other programs it shouldn't be using inheritance. Composition makes life much easier. – Voo Mar 16 '12 at 18:00
-
@prajeesh kumar answer to this question must be correct and wrotten in English (there is my dis_advantage) language – mKorbel Mar 16 '12 at 18:02
1 Answers
6
Usually it's a rule of thumb to only subclass if you need to customize the Swing component.

Moonbeam
- 2,283
- 1
- 15
- 17
-
@KirkWoll, As in add behavior or LaF not native to the component, or see [this](http://dictionary.reference.com/browse/customize?s=t). – Moonbeam Mar 16 '12 at 17:51
-
3
-
1@mKorbel Good you added the key term 'Composition versus Inheritance', since that is what it comes down to. – Andrew Thompson Mar 16 '12 at 18:10
-
@KirkWoll Usually that would be a necessary override of at least one method. – Tom Hawtin - tackline Mar 21 '12 at 16:08