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?

7
  • 1
    "what makes it bad?" Why do you think it's bad?
    – Kirk Woll
    Mar 16, 2012 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, 2012 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, 2012 at 17:48
  • 2
    @Bob Who are those people? I've never heard that.
    – Maxpm
    Mar 16, 2012 at 17:49
  • 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, 2012 at 18:00

1 Answer 1

6

Usually it's a rule of thumb to only subclass if you need to customize the Swing component.

4
  • @KirkWoll, As in add behavior or LaF not native to the component, or see this.
    – Moonbeam
    Mar 16, 2012 at 17:51
  • 3
    everything is described java Composition versus Inheritance
    – mKorbel
    Mar 16, 2012 at 17:56
  • 1
    @mKorbel Good you added the key term 'Composition versus Inheritance', since that is what it comes down to. Mar 16, 2012 at 18:10
  • @KirkWoll Usually that would be a necessary override of at least one method. Mar 21, 2012 at 16:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.