In Java Swing, should I extend Jpanel and top-level containers like Jframe?

Asked

Viewed 42 times

5

Has in the Soen but I was not satisfied with the answers, and temporarily I am not able to ask IF..

Top-level containers like the JFrame I guess there’s no need, but a JPanel kind of made to be an abstract thing, and therefore maybe extensible.

Shall I extend it? The rule says that only when will extend (override) some method or (maybe) extend its functionality, and have to respect Liskov.

A Jpanel with a set of buttons (a button panel) deserves to be extended? One with business entities (has a name for them, UI model or something) deserves to be extended? With business methods?

Don’t get plastered after?

1 answer

4


Prefer to use composition rather than inheritance if possible. This way, you could create classes that contain several types fields JPanel, JButton, JRadioButton, etc. and use them.

If you really want to go through the inheritance, JPanel is a relatively clean and easy to inherit class, it will hardly cause you any problems. But although you may extend JPanel easily, the very JPanel ends up being a somewhat innocuous class because it is a practically empty subclass. In that case, it would be more worth inheriting from JComponent, which is the superclass of JPanel.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.