I've a JFrame
'A'
and I'd like to provide a very similar one, but with extra buttons, and different functionality. I was wondering if I should inherit 'A'
and then overwrite the handler methods, and add components, or whether a different approach is recommended.
views:
60answers:
3Inheritance is the best way here.
Honestly, doing it another way would involve a lot of work.
Prefer composition over inheritence in most cases. In this case it depends on how much code there is in A. You could either use a Strategy pattern for the variability points in the code, but in most cases inheritence will be the least ugly.
Personally I would prefer inheriting JPanels. I find that the reuse aspect leans more towards using the UI Panels and reusing those.
The way I build my Swing UIs is to have a JFrame and have it use a panel that is composed of reusable panels if they share common functionality.
If you have a JFrame that shares common functionality then you might want to create one with a given CloseHandler and other common code and then have your other Jframes inherit from that common one.