Javax Swing Interfaces and Generics

G

Graeme

Hi,

It would be quite nice to have generic method parameters in the Swing
interfaces, for instance, looking at:

http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreeModel.html

Every object that is referred to in the interface declaration is type
Object. What if I want to make that T instead, or T extends
MyInterface?

What would be the best way to implement a generic interface to a Swing
interface - I guess just extend it and make the interface declaration
there?

What are your thoughts?

Graeme.
 
A

Arne Vajhøj

Graeme said:
It would be quite nice to have generic method parameters in the Swing
interfaces, for instance, looking at:

http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreeModel.html

Every object that is referred to in the interface declaration is type
Object. What if I want to make that T instead, or T extends
MyInterface?

What would be the best way to implement a generic interface to a Swing
interface - I guess just extend it and make the interface declaration
there?

What are your thoughts?

SUN did not do it because the class predates generics.

You could create a generic wrapper.

Pro: type safe

Con: replaces the use of a standard class with a custom class

I don't think I would do it.

Arne
 
M

markspace

Arne said:
SUN did not do it because the class predates generics.

You could create a generic wrapper.


I'm not sure how a wrapper would work. If you try to override a method
with a generic parameter, you no longer override it but overload it
instead. It's a bit of a catch 22. You could parameterize the return
types however; everything is covariant with Object. ;)

Some kind of builder pattern might work. Make a generic builder, then
have it make the TreeModel for you, but that won't change the fact that
the basic TreeModel still deals with Objects once you get it. Maybe a
proxy that operates on the base builder object for you.
 
R

Roedy Green

What would be the best way to implement a generic interface to a Swing
interface - I guess just extend it and make the interface declaration
there?

Look at how Sun generified the Collections. They managed to make them
continue to work without generics. You would have to do something
similar for Swing.

They modified the original code. They did not pull it off with a
simple set of wrapper subclasses.

Rather than creating a generalised implementation, you might just wrap
a class with a particular class built in, WITHOUT generics.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Patriotism is fierce as a fever, pitiless as the grave, blind as a stone, and as irrational as a headless hen."
~ Ambrose Bierce (born: 1842-06-24 died: 1914 at age: 71)
 
M

markspace

Roedy said:
Rather than creating a generalised implementation, you might just wrap
a class with a particular class built in, WITHOUT generics.


Again, because of the way polymorphism works, it's impossible to
actually provide a wrapper for (for example) TreeModel that overrides
the methods needed instead of overloading them.

It does occur to me that there is a second pattern in the Adapter family
(which includes Wrapper) that might be useful. You could define your
own, totally separate interface:

// A very simple interface
interface FooModel {
void setFooInTree( Foo f, index i );
void Foo getFooFromTree( index i );
}

And then wrap that with a TreeModel, using the Class Adapter pattern.
Kind of the reverse of what's being suggested with the Wrapper pattern.

class FooTreeModel extends DefaultTreeModel implements FooModel {

// implement FooModel here in terms of TreeModel...

}

This will still expose the TreeModel interface (which you must, to get
it to work with a JTree), but as long as your controllers, data models,
and such-like are coded to and tested against FooModel, you should
achieve reasonable separation and not be bothered by the extra methods
in FooTreeModel.

Just my 2 cents.
 
D

Daniel Pitts

Graeme said:
Hi,

It would be quite nice to have generic method parameters in the Swing
interfaces, for instance, looking at:

http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreeModel.html

Every object that is referred to in the interface declaration is type
Object. What if I want to make that T instead, or T extends
MyInterface?

What would be the best way to implement a generic interface to a Swing
interface - I guess just extend it and make the interface declaration
there?

What are your thoughts?

Graeme.
TreeModel is difficult, because often your nodes are not homogeneous,
but are rather different kinds depending both location and depth.

ListModel on the other hand, can be made to work. I once wrote a class
that implemented List<T>, RandomAccess, and ListModel. What you might
consider doing is to make your TreeModel class actually access a real
domain object graph, instead of putting domain data into a TreeModel
instance.
 
D

Daniel Pitts

markspace said:
Again, because of the way polymorphism works, it's impossible to
actually provide a wrapper for (for example) TreeModel that overrides
the methods needed instead of overloading them.

It does occur to me that there is a second pattern in the Adapter family
(which includes Wrapper) that might be useful. You could define your
own, totally separate interface:

// A very simple interface
interface FooModel {
void setFooInTree( Foo f, index i );
void Foo getFooFromTree( index i );
}

And then wrap that with a TreeModel, using the Class Adapter pattern.
Kind of the reverse of what's being suggested with the Wrapper pattern.

class FooTreeModel extends DefaultTreeModel implements FooModel {

// implement FooModel here in terms of TreeModel...

}

This will still expose the TreeModel interface (which you must, to get
it to work with a JTree), but as long as your controllers, data models,
and such-like are coded to and tested against FooModel, you should
achieve reasonable separation and not be bothered by the extra methods
in FooTreeModel.

Just my 2 cents.

TreeModel really should not be used to store your app-data. Do you
really want to couple your Domain model with Swing classes? Instead,
you should create TreeModel implementation that queries your domain model.
 
J

Joshua Cranmer

Graeme said:
Hi,

It would be quite nice to have generic method parameters in the Swing
interfaces, for instance, looking at:

http://java.sun.com/javase/6/docs/api/javax/swing/tree/TreeModel.html

Trees and tables, to a large extent, carry heterogeneous data--much more
than lists or maps, which overwhelmingly store the same data. For
example, a certain table I'm looking at would be displaying (if it were
implemented with a Java JTable), in order, an enum, a boolean, a String,
boolean, String, boolean, and Date. So what would my generics parameter be?

My bigger pet peeve is the use of Vector and Hashtable in Swing instead
of the more correct List and Map.
 
B

Bill McCleary

Joshua said:
Trees and tables, to a large extent, carry heterogeneous data--much more
than lists or maps, which overwhelmingly store the same data. For
example, a certain table I'm looking at would be displaying (if it were
implemented with a Java JTable), in order, an enum, a boolean, a String,
boolean, String, boolean, and Date. So what would my generics parameter be?

Object. :)
My bigger pet peeve is the use of Vector and Hashtable in Swing instead
of the more correct List and Map.

Don't forget Enumeration. ListenerList and several other AWT/Swing
classes use that ancient piece of cruft too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top