如何在RichTextFx中设置垂直滚动(How to set the vertical scroll in RichTextFx)

编程入门 行业动态 更新时间:2024-10-27 06:27:19
如何在RichTextFx中设置垂直滚动(How to set the vertical scroll in RichTextFx)

我希望能够将垂直滚动设置为RichTextFX中 InlineStyleTextArea的顶部或底部。 通过这个线程的外观, moveTo(..)应该可以解决问题。 但它对我不起作用。 我也尝试过selectRange(..)和positionCaret(..) 。 下面是我的测试程序,我是否误解了上面链接的线程中提到的“重新定位插入符的解决方法”?

import org.fxmisc.richtext.InlineStyleTextArea; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class RichTextFXTest extends Application { @Override public void start(Stage stage) { InlineStyleTextArea<StyleInfo> area = new InlineStyleTextArea<>( new StyleInfo(), styleInfo -> styleInfo.toCss()); area.setPrefSize(300, 300); area.setWrapText(false); area.setEditable(false); StringBuilder largeText = new StringBuilder(); for (int i = 0; i < 5000; i++) { largeText.append("Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n"); } area.replaceText(largeText.toString()); // None of the two rows below works. //area.selectRange(1000, 1100); //area.moveTo(1100); //area.positionCaret(1100); HBox rootLayout = new HBox(); rootLayout.setPrefSize(300, 300); rootLayout.getChildren().add(area); Scene scene = new Scene(rootLayout); stage.setScene(scene); stage.show(); } // Style class class StyleInfo { String toCss() { return "-fx-fill: red; } } public static void main (String[] args) { launch(); } }

I want to be able to set the vertical scroll to the top or the bottom of a InlineStyleTextArea in RichTextFX. By the looks of this thread, moveTo(..) should do the trick. But it doesn't work for me. I've also tried selectRange(..) and positionCaret(..). Below is my test program, have I misunderstood the "workaround of repositioning the caret" mentioned in the thread linked above?

import org.fxmisc.richtext.InlineStyleTextArea; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class RichTextFXTest extends Application { @Override public void start(Stage stage) { InlineStyleTextArea<StyleInfo> area = new InlineStyleTextArea<>( new StyleInfo(), styleInfo -> styleInfo.toCss()); area.setPrefSize(300, 300); area.setWrapText(false); area.setEditable(false); StringBuilder largeText = new StringBuilder(); for (int i = 0; i < 5000; i++) { largeText.append("Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n"); } area.replaceText(largeText.toString()); // None of the two rows below works. //area.selectRange(1000, 1100); //area.moveTo(1100); //area.positionCaret(1100); HBox rootLayout = new HBox(); rootLayout.setPrefSize(300, 300); rootLayout.getChildren().add(area); Scene scene = new Scene(rootLayout); stage.setScene(scene); stage.show(); } // Style class class StyleInfo { String toCss() { return "-fx-fill: red; } } public static void main (String[] args) { launch(); } }

最满意答案

@Tomas Mikula在这个 github线程中提供了答案。 我以为我也会把答案放在这里。

你的例子的问题是moveTo / selectRange解决方法在实例化皮肤之前不起作用。 如果在您的示例中,您在Platform.runLater中包围了selectRange,那么将为您提供所需的结果。 另请注意,范围以字符索引给出,而不是行号。

Platform.runLater(() -> area.selectRange(10000, 10100));

在文本区域显示后,用户交互产生的代码中,您将不再需要Platform.runLater。

@Tomas Mikula provided the answer in this github thread. I thought I'd put the answer here as well.

The problem with your example is that the moveTo/selectRange work-around does not work before the skin is instantiated. If in your example you surround the selectRange in Platform.runLater, that will give you the desired result. Also note that the range is given in character indices, not line numbers.

Platform.runLater(() -> area.selectRange(10000, 10100));

In the code that results from user's interaction after the text area has already been shown, you will not need Platform.runLater anymore.

更多推荐

本文发布于:2023-07-31 05:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1341904.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   RichTextFx   set   vertical   scroll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!