For my Swing project, I need to support both Java 5 and Java 6.
I have defined a custom JComponent
(call it Picture
) and, after embedding it in a JScrollPane
, I put it in a JPanel
that uses DesignGridLayout manager.
DesignGridLayout supports baseline alignment thanks to swing-layout open source library (implements baseline support for Java 5 and provides compatibility with the new Java 6 baseline support).
My Picture
class overrides public int getBaseline(int width, int height)
so that I can define a correct baseline for it. Note that "override" is not completely correct: it overrides the method on Java6 but defines it in Java5.
When I run my sample app on Java5, everything is fine: the Picture
baseline I have defined is correctly used.
However, when I use Java6, my Picture#getBaseline()
method does not get called! And of course the baseline alignment of my picture is terrible (centered).
After checking in Java6 source, I have seen that, in BasicScrollPaneUI
, getBaseline()
calls first getBaselineResizeBehavior()
on the viewport component (my Picture
instance).
And it will call getBaseline()
only if getBaselineResizeBehavior()
returns Component.BaselineResizeBehavior.CONSTANT_ASCENT
.
Now my problem is that getBaselineResizeBehavior()
is a Java6 method of JComponent
that I cannot implement in Java5 because it returns an enum Component.BaselineResizeBehavior
which does not exist in Java5.
So my question (finally) is: how can I implement (or simulate?) getBaselineResizeBehavior()
so that my class can still compile and run in a Java5 environment?