Question

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 :-)

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top