wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Doubts regarding AWT/Swing Design Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Doubts regarding AWT/Swing Design" Watch "Doubts regarding AWT/Swing Design" New topic
Author

Doubts regarding AWT/Swing Design

sarathy
Greenhorn

Joined: Oct 03, 2005
Posts: 4
Hi all,
I have good programming exp. in JAVA. But it seems that i dont get to understand the AWT/Swing design fully. Consider the following 2 cases.

java.lang.Object
extended by java.awt.Component
extended by java.awt.Button

java.lang.Object
extended by java.awt.Component
extended by java.awt.Container
extended by javax.swing.JComponent
extended by javax.swing.AbstractButton
extended by javax.swing.JButton

I believe that a component is a widget such as Button, Label etc ..
How come in AWT that the Component is directly a subclass of Object.

But in Swing it comes well below in the heirarchy. I was wondering if this design was correct.

Also the inheritance logic does'nt seem to work well in the second case.
How can a JButton class ever be a subclass of Container? class. A container is one which contains other components, right?

How can a Container class be a subclass of Component class ?

Please clarify,
Sarathy
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by sarathy:
I was wondering if this design was correct.


Well, it's not pretty, but it works. Seems like it was the best that came to the mind of the Sun developers. There might have been more elegant solutions, but I guess they also would have been more complex.

It's a compromise between elegance and simplicity, I think.


How can a JButton class ever be a subclass of Container? class. A container is one which contains other components, right?


You are right, that doesn't make much sense, semantically.

The problem is that the Sun developers wanted to reuse the AWT code for Swing by inheritance (which makes kind of sense). It would have made more sense for JComponent to inherit from Component, and to have a JContainer class that inherits from the AWT Container. But then JContainer also had to inherit from JComponent at the same time (see below), which cannot be done in Java (because Java doesn't allow mutliple implementation inheritance).


How can a Container class be a subclass of Component class ?


It's the Composite design pattern. A Container is a component that is composed of other components. And those other components can be containers again (because containers are components). Very elegant.

Does that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

"sarathy", welcome to the ranch. Please take a few moments to read the Javaranch naming policy. After reading the policy, you can change your name here. We try to be friendly around here but we do have our rules, and not following the naming policy can result in having your account deleted.

We'd prefer to keep you with us.


A good workman is known by his tools.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Component and Container should have been interfaces. The problem is that when AWT was originally designed, nobody had much experience programming in Java. The core APIs are full of this sort of thing -- design decisions that were obvious mistakes, but now it's too late to do anything about it.

It's kind of funny if you think about it -- the Java code used by the largest number of Java developers are the APIs themselves, and yet this is some of the least idiomatic Java code in existience, since it was written by people who were brand-new to working in Java!


[Jess in Action][AskingGoodQuestions]
 
Don't get me started about those stupid light bulbs.
 
subject: Doubts regarding AWT/Swing Design