1

I have a Groovy app which uses a scrollPane built via swing builder:

BinsicWindow(def controller)
{
    controlObject = controller
    swinger = new SwingBuilder()
    mainFrame = swinger.frame(
        title: "Binsic is not Sinclair Instruction Code",
        size:[640, 480],
        show:true,
        defaultCloseOperation: WindowConstants.DISPOSE_ON_CLOSE){
        scrollPane(autoscrolls:true) {
            screenZX = textArea(rows:24, columns:32) {visble:true}
        }
        screenZX.setFont(new Font("Monospaced", Font.PLAIN, 18))
    }
}

I add text to the textArea programatically (i.e. no user input) and I would like the textArea to scroll down automatically as content is added. But the view remains fixed at the top and I can only see the bottom (once the screen is more than full) by dragging the mouse.

Can I fix this? I have been searching for an answer to this for a wee while now and getting nowhere. Apologies if it's a simple answer.

adrianmcmenamin
  • 1,081
  • 1
  • 15
  • 44

1 Answers1

2

The following lines should scroll your textarea to the last text position:

rect = screenZX.modelToView(screenZX.getDocument().getLength() - 1);
screenZX.scrollRectToVisible(rect);
Hakan Serce
  • 11,198
  • 3
  • 29
  • 48
  • OK, I could call that every time I add text, but is there a way to set the scrollPane to do this automatically? – adrianmcmenamin Jun 06 '12 at 20:49
  • You can put this code to a `DocumentListener`. This would make scrolling automatically whenever text is changed. But if you ask whether there is a method like "setAutoScrollToLastLine(true)", there is no such thing as far as I know. – Hakan Serce Jun 06 '12 at 20:55