JavaHR
JavaHR

Reputation: 193

JavaFX ScrollPane - Scroll down on update

I have made a ScrollPane which contains a TextArea. When the TextArea gets additional text with the push() method, the scroll bar needs to be set to the bottom. However, when push() is used, the bar sets to the top instead. I am not sure what methods are needed to set it to the bottom.

Here is the ScrollPane code:

package Games.UI.RPG;

import Games.Other.Constants;
import javafx.event.EventHandler;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.input.ScrollEvent;

/**
 * Scrollable window which contains Text messages.
 */
class LogPane extends ScrollPane{

  private TextArea log;

  LogPane(){
    initialize();
  }

  private void initialize(){
    log = new TextArea();
    log.setEditable(false);
    log.prefWidth(512);
    log.setText("Text Log loaded. Messages:");

    setContent(log);
    setFitToWidth(true);
    setFitToHeight(true);

    setMinHeight(Constants.height);
    setMaxHeight(Constants.height);
    setMinWidth(Constants.width / 3);
    setMaxWidth(Constants.width / 3);
  }

  public void push(String message){
    log.setText(log.getText() + "\n" + message);
    setVvalue(getVmax());
  }
}

Upvotes: 1

Views: 1019

Answers (1)

SedJ601
SedJ601

Reputation: 13859

As user1803551 pointed out, you are using a ScrollPane and a TextArea. TextArea has a scroll when it's text get long. So you need to move the TextArea scroll to the bottom because the ScrollPane's scroll is never shown.

public void push(String message)
{
    log.setText(log.getText() + "\n" + message);
    log.selectPositionCaret(log.getLength());
    log.deselect();
}

Other ways of moving the TextArea scroll to bottom here. I personally prefer log.appendText();.

Upvotes: 1

Related Questions