JavaFX:如何滚动到文本区域中的特定行

时间:2018-07-01 11:54:39

标签: javafx

package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import java.util.stream.IntStream;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    final StringBuilder sb = new StringBuilder();
    IntStream.range(1, 100).forEach(i -> sb.append("Line " + i + "\n"));

    TextArea ta = new TextArea();
    ta.setText(sb.toString());

    //how to I get line 30 at top of the visible textarea
    double someValue = 0;
    ta.setScrollTop(someValue);

    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(ta, 300, 300));
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}

}

如何在可见的文本区域顶部获得第30行? 我认为“ someValue”应相对于可以滚动的总高度 但是可以滚动的总高度是多少

这是我想要达到的结果:
This is the result I want to achieve

1 个答案:

答案 0 :(得分:1)

这有点棘手。我们只能确定每行的高度并调用ta.setScrollTop((line - 1) * lineHeight);,但是我们不知道TextArea使用什么行距。

但是我发现TextAreaSkin包含用于确定任何选定字符边界的公共方法,我们只需要知道其索引即可。

@Override
public void start(Stage primaryStage) throws Exception{
    final StringBuilder sb = new StringBuilder();
    IntStream.range(1, 100).forEach(i -> sb.append("Line " + i + "\n"));

    TextArea ta = new TextArea();
    ta.setText(sb.toString());

    // TextArea did not setup its skin yet, so we can't use it right now.
    // We just append our task to the user tasks queue.
    Platform.runLater(() -> {

        // Define desired line
        final int line = 30;

        // Index of the first character in line that we look for.
        int index = 0;
        // for this example following line will work:
        // int index = ta.getText().indexOf("Line " + line);

        // for lines that do not contain its index we rely on "\n" count
        int linesEncountered = 0;
        boolean lineFound = false;
        for (int i = 0; i < ta.getText().length(); i++) {
            // count characters on our way to our desired line
            index++;

            if(ta.getText().charAt(i) == '\n') {
                // next line char encountered
                linesEncountered++;
                if(linesEncountered == line-1) {
                    // next line is what we're looking for, stop now
                    lineFound = true;
                    break;
                }
            }
        }

        // scroll only if line found
        if(lineFound) {
            // Get bounds of the first character in the line using internal API (see comment below the code)
            Rectangle2D lineBounds = ((com.sun.javafx.scene.control.skin.TextAreaSkin) ta.getSkin()).getCharacterBounds(index);

            // Scroll to the top-Y of our line
            ta.setScrollTop(lineBounds.getMinY());
        }
    });

    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(ta, 300, 300));
    primaryStage.show();
}

此解决方案适用于Java 8(9+版本),TextAreaSkin已移至公共软件包,因此,使其生效所需要做的就是将com.sun.javafx.scene.control.skin.TextAreaSkin替换为javafx.scene.control.skin.TextAreaSkin