user4780686
user4780686

Reputation:

Java Swing Organization

My assignment says to "identify and fix any errors". This image, which was taken from a Java textbook shows the organization of Swing structure. This looks fine to me and I do not see any issue.

Can somebody explain this?

Should JPanel go before JComponent?

enter image description here

Upvotes: 2

Views: 248

Answers (2)

Shai Almog
Shai Almog

Reputation: 52760

The reason for this "broken" hierarchy in Swing where all components are derived from AWT's Container and thus have methods like add(Component) that make no sense is historic.

Swing was shipped initially as a 3rd party JAR that needed to download into JDK 1.1 browsers and work there. So it needed to work on top of JDK 1.1 AWT. This was the design they chose to enable that.

Modern GUI frameworks that ape Swing e.g. Codename One or JavaFX skipped this compromise which is one of the pain points of Swing.

Upvotes: 1

Holger
Holger

Reputation: 298599

This obviously is a trick question, indicated by the fact that nobody noticed the problem so far and there were quite experienced developers commenting your question.

The problem is that the arrow between Component and Container points into the wrong direction, but since the vertical placement follows the typical pattern of placing base classes above the subclasses, it’s easy to overlook. I wouldn’t call something that focuses that way on an easy-to-overlook graphical detail a serious question.

Besides that, you could improve the tree by inserting AbstractButton and JTextComponent into the inheritance hierarchy tree and add more of the missing front-end classes, but the absence of these classes is not a real error considering the graphic a simplified tree.

You can compare the tree with the actual inheritance tree in the official documentation.

Upvotes: 3

Related Questions