• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overriding Swing primitives

 
Malcolm Storey
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some way of getting from a JFrame or JInternal frame (as returned by Swing) back to my composition object which manages it. Am currently doing this by overriding (eg) JFrame with a small subclass (MyJFrame) which stores a reference to its manager.

Having been dipping into Bloch's excellent "Effective Java", and now understand the dangers of overriding objects from external libraries. Should I use a HashMap to link the two (seems unnecessarily cumbersome) or is there a better way?

Many thanks
 
Malcolm Storey
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody's come up with an answer, so did a bit of what nowadays is called "research" (ie Googling).

Sun's Java Technology Fundamentals Newsletter offers the following:
http://java.sun.com/new2java/supplements/2002/jan02.html

"Even more common, though, is using the keyword extends with a child class to JFrame to gain the benefits of inheritance".

But beware that it has a finalize() method - see Bloch, p20-24 if you ever want to override finalize().

Turning to JInternalFrame,
Java2S offers an example extending JInternalFrame:
http://www.java2s.com/Code/JavaAPI/javax.swing/extendsJInternalFrame.htm

JInternalFrame has only the trivial finalize() inherited from Object.

In conclusion: looks like it's OK to subclass both JFrame and JInternalFrame.

My code for JInternalFrame is more complete than that for JFrame: the subclass is only needed to identify the active subwindow, and while walking the desktop's component list when tidying subwindows. Most of the rest of Swing returns its state-changes via listeners, and it maybe that with more effort I could track what I need via listeners to JFrame and JInternalFrame.
[ November 29, 2008: Message edited by: Malcolm Storey ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!