Re: Method returns interface?
Radwanski wrote:
Hi,
I am trying to understand the working of JTable.
But JTable is more complex as I expected.
I also am a little bit confused by the Java documentation (downloaded
from Sun)
If I look at JTable.getColumnModel() it seems to return an Interface.
As far as I know this is not possible.
It can return a class that implements the interface.
Is it today possible that an method returns an interface?
A method can return nothing (void), or a primitive value
(int, double, ...), or a reference that is either null or refers
to an object instance. When a method returns a reference, it is
declared as returning a reference to some particular kind of
object: "I return a String" or "I return a Dog" or "I return
a Pettable."
When a method says "I return a Dog," it means that it returns
a reference to any object that satisfies the definition of a Dog.
That object might be an instance of the Dog class, or it might
be an instance of a Dog subclass like Terrier or of Schnauzer or
Pekinese. Since all these things are Dogs in addition to being
Wolfhounds or whatever, the method can return a reference to any
of them and still keep its promise "I return a Dog."
When a method says "I return a Pettable," it means that it
returns a reference to any object that satisfies the definition
of a Pettable. That object might be an instance of Dog, or of
Cat, or of HeavyDate, classes that implement the Pettable interface
and therefore can be called Pettable. Since all of these things
implement Pettable (in addition to other interfaces they might
implement, like Ownable or Marriageable), the method can return
a reference to any of them and still keep its promise "I return
a Pettable."
That's the sense in which a method can "return an interface:"
the method promises that the reference it returns will either be
null or will refer to an object whose class implements the stated
interface. And there's no "today" about it; that's the way Java
has been since the beginning.
I simply want a table where I can directly set the headers and cell
formats.
I allready created a table and now want to change header allignment.
Have you looked at the on-line Java Tutorial? If I recall
correctly, it has a section on how to set up custom renderers (and
custom editors) for JTable cells.
Also later need to merge (collspan) cells but doubt that it is
possible with JTable.
I'm not sure. I don't recall seeing any such capability, but
on the other hand I've never looked for it. (See point 3 below.)
A tried html in Java (JEditorPane) but html and css support is too
limited.
I can't create a good looking output on the screen.
Basicly I just need to output Strings in a table.
(Balance and P&L on the screen)
Any idea's how this can be done easily in Java.
JTable has always seemed to me a rather baroque class, something
with far more features than anyone would ever want. (On the other
hand, if you asked twenty people what features of JTable they simply
could not do without, each person's list might be short but their
union might be surprisingly long.) For me, the lessons on using
JTable boil down to:
1) Be very strict about separating the JTable from the TableModel,
and be sure to put the right functionality into each. If the JTable
accesses the data directly or if the TableModel gets involved with
what the JTable looks like, you've done something wrong.
2) Start from simplicity and work up to the required complexity.
Begin with a dead-simple JTable that just displays everything in the
default formats, without any special presentation or interaction.
Get that simple JTable and TableModel working correctly (if not
very prettily), and *then* start adding the bells and whistles: The
custom renderers and/or editors, the sorting widgets, the row filters,
and so on.
3) At each stage of initial crude development or later refinement,
get a clear idea of the immediate goal and then plow through the
JTable documentation and tutorials with that goal in mind. Ignore
anything that doesn't bear directly on the immediate task, because
there is so much of it you will become overwhelmed. Just put your
head down and focus on the task at hand; you'll make another pass
later to accomplish the next task.
After you've done a few, I think you'll find that JTables need
not be quite as intimidating as they seem at first.
--
Eric.Sosman@sun.com