I took a little time today to add some new features to my "JustWrite" text editor, and one of the features I'm experimenting with is an animation similar to the current horizontal scrolling area shown at the top of Apple's Mac web page. When you first hit that page the scrolling area is offset a little bit, and then in an animation the scrollbar moves more to the center of the scrolling display area.
In short, I'd like to do the same thing in a Java scrolling textarea. Assuming I have a document that is multiple pages in length, I'd like to (a) initially display the JTextArea JTextPane or JEditorPane in an area that is slightly offset from the last known cursor location, and then (b) scroll the text area so the last cursor position is centered in the middle of the viewing area.
For the record, JustWrite is a free, full-screen text editor for Mac OS X systems, and it looks like this:
(Oh, and Mr. Paul Allen, if you try to patent this idea in the future, I'm claiming "prior art" here.)
Getting a reference to the JScrollBar in Java
I don't know exactly how to do this yet, but I have worked out the part of accessing the vertical scroll bar. I get the vertical scrollbar (a JScrollBar) from my JScrollPane using code that looks a little like this:
// get a reference to the vertical scroll bar JScrollBar scrollBar = mainFrame.getScrollPane().getVerticalScrollBar();
Java JScrollBar example source code
I normally don't post incomplete source code out here, but unfortunately at this time I don't know exactly where to go with this from here, but I do know there are a variety of getter methods on the JScrollBar that look like this:
int val = scrollBar.getValue(); int max = scrollBar.getMaximum(); int min = scrollBar.getMinimum(); int unitIncrement = scrollBar.getUnitIncrement();
I've also learned that setting the value on the scrollbar looks something like this:
SwingUtilities.invokeLater(new Runnable() { public void run() { scrollBar.setValue(theValue); } });
In this code I've skipped how I'm trying to calculate the variable named theValue
, as I don't have this worked out right. Also, I know this will do nothing to actually seem like an animation. I know I need to put this inside a loop, something this following code, to make this happen:
SwingUtilities.invokeLater(new Runnable() { public void run() { for (int i=something; i<somethingElse; i = i + someIncrement) { scrollBar.setValue(theValue); } } });
However, all of those variables are mysteries to me at this point.
I hope to have a nice, complete Java JScrollBar example out here in the near future, but until then, if you just need to learn about the basics of the Java JScrollBar, here's a link to a Sun/Oracle scrollable page.