views:

492

answers:

1

Hiya - quick one - is there any harm / value in subclassing JComponent as compared to JPanel ?

To me they pretty much look to be the same thing if I'm doing my own drawing & the object won't have any children, however there seems to be a pref for subclassing JPanel over JComponent - just looking for opinions on why this might be ...

Thx :-)

+6  A: 

If you are drawing the whole component yourself, then use JComponent. JPanel is just a simple concrete instance of JComponent (which is abstract), and is not really meant to have its methods overridden.

JPanel is sometimes subclassed so that the subclass constructor can add various controls/layout rather than having to do it via some method call.

Richard
Many people thing JPanel adds setOpaque(false), but that actually depends upon which look and feel is being used.
Tom Hawtin - tackline