How to determine if a JScrollPane's vertical scrollbar is visible/necessary/drawn?
Posted on 2004-04-07
I have scoured the web far and wide, and have finally given up, I think. What I want to do is conceptually very simple, but I have yet to find a method for implementing it that produces the desired results. Here's the scenario:
In my JApplet, I have a JScrollPane wrapping a JTextArea. I have the scrollbar policy set to JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED (the default). I also have a button in my applet, which I want to conditionally enable only if (a) there no scrollbars are required for the JScrollPane or if (b) the scrollbar position is at the very bottom of the text area.
I've been able to accomplish the latter piece by code in an adjustmentValueChanged() event handler (my applet implements AdjustmentListener) that looks like this:
JScrollBar scrollBar = (JScrollBar)e.getSource();
int value = scrollBar.getValue();
int max = scrollBar.getMaximum();
int extent = scrollBar.getModel().getExtent();
if ((value != 0) && (value == (max - extent)))
// code to disable button goes here
However, I have been unable to figure out how to accomplish part (a), because I have not been able to figure out how to programmatically determine if the vertical scrollbar is even drawn/necessary. Here's several things I've tried:
- JScrollPane.getVerticalScrollBar() returns a non-null JScrollBar! (why, even when no scrollbar is drawn???)
- JScrollBar.isValid() (on the scrollbar that's returned from getVerticalScrollBar()) returns true even when there is no scrollbar
- JScrollBar.isVisible() returns false even when there *is* a scrollbar
- JScrollBar.getMaximum() always returns a positive value (not 0 or anything simple) even when there is no scrollbar
So - the question to all you experts out there - how on *EARTH* can I possibly do this? Am I being blind and missing the obvious? Also, please keep in mind that I am developing for JVM 1.3.x and higher - all solutions have to be targeted for Java 1.3.
Help!!! (This question is both urgent *and* extremely difficult, at least to me, hence the 500 pts! :-)