Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java swing : custom everything - subclass jcomponent or jpanel or ...?

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

like image 280
Dave Carpeneto Avatar asked Apr 20 '26 03:04

Dave Carpeneto


1 Answers

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.

like image 82
Richard Avatar answered Apr 21 '26 17:04

Richard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!