4

As per the class hierarchy in java.awt.*, class Button & class Label is-a class Component, and Component is not a Container, which make sense to me.

As per the redesign of class hierarchy in javax.swing.*, class JButton is-a class JComponent in-turn class JComponent is-a class Container,

So, What does it mean to say that, class JButton or class JRadioButton is-a class Container? How could one think of using button or radiobutton as container in GUI programming?

Note: I am java beginner.

overexchange
  • 2,245
  • 2
  • 17
  • 47

1 Answers1

3

What does it mean to say that, class JButton or class JRadioButton is-a class Container?

The reason why all Swing components are derived from java.awt.Container is mostly for practical reasons which are internal to the implementation of Swing. AWT and Swing usually will not be mixed. But internally, a Swing component might realize itself using more than one AWT component, and that's purely up to the Swing component itself.

How could one think of using button or radiobutton as container in GUI programming?

The fact that Swing components extend java.awt.Container can and should mostly be ignored. One shouldn't mess around with the java.awt.Container methods of Swing components unless it's obvious that it makes sense (which is the case for most if not all *Pane components and javax.swing.JPanel).

Christian Hujer
  • 2,365
  • 1
  • 10
  • 14
  • `practical reason`?, any class hierarchy that depicts `is-a` relation should actually mean it. Is this the design failure? For me, `java.awt` class hierarchy looks right in this aspect. – overexchange Nov 30 '14 at 16:46
  • 1
    I'd also call it design failure. The fact that `javax.swing.JButton` is-a `Container` violates the Liskov Substitution Principle. When Java was designed, the Liskov Substitution Principle already existed for more than 2 decades, but only recently has it become popular, partially also thanks to Robert C. Martin. – Christian Hujer Nov 30 '14 at 18:58
  • am sorry, i do not know much about LSP, but how is that relavant to my question, can you quickly help me on this? – overexchange Dec 01 '14 at 05:55
  • 1
    @overexchange: It is relevant to your question in the sense that you shouldn't search for too much meaning why certain choices were made in the design of Swing and that the choices made there are not always considered the best choices today. – Bart van Ingen Schenau Dec 01 '14 at 08:47
  • @BartvanIngenSchenau So, From this apsect, Do you suggest better technology, other than Swing/awt that is better designed for thick client java programming? – overexchange Dec 01 '14 at 10:22
  • 1
    @overexchange: I don't suggest anything of the sort. First of all, I am not familiar enough with Jave to know which technologies exist. And most importantly, the fact that some technology doesn't follow current best practices doesn't render that technology completely useless (it only becomes useless as a teaching tool for current best practices). – Bart van Ingen Schenau Dec 01 '14 at 10:28
  • @overexchange LSP is relevant to your question because the LSP is your question at its core - "Why is a `JButton` a `Container`?" is a valid question, and when you can't use a `JButton` as a `Container`, `JButton` cannot replace all instances of `Container`, and that's a violation of the LSP. – Christian Hujer Dec 01 '14 at 20:33