3

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Bob
  • 417
  • 2
  • 7
  • 13
  • 1
    "what makes it bad?" Why do you think it's bad? – Kirk Woll Mar 16 '12 at 17:46
  • 1
    Who 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 Answers1

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