【发布时间】:2014-04-07 18:06:33
【问题描述】:
我遇到了一个看起来确实很奇怪的情况。
下面是一个简单的 javaFX 应用程序,它在 ScollPane 中包含一个 TextFlow 容器。为了在将文本添加到 TextFlow 容器时将 ScrollBar 发送到 ScrollPane 的底部,对 TextFlow 容器的 heightProperty 进行了 DoubleProperty 绑定(在最近添加 Text 后 TextFlow 容器呈现后更改),使用添加了一个 ChangeListener 以将 ScollPane 的 vValue 移动到 vMax(底部)。
添加按钮只是测试将文本添加到 TextFlow 容器。
现在奇怪的部分 - ChangeListener 完美地适用于前 9 到 11 个文本添加,之后它似乎没有通过绑定触发 heightProperty 更改(好吧,至少 ChangeListener 不再执行)。在 9 次、10 次还是 11 次之后发生这种情况似乎会随着点击 Add 按钮的速度而有所不同。
现在更奇怪的部分 - 当应用程序在调试中运行时,ChangeListener 可以完美运行(不需要断点)!
我在 Windows 8.1 笔记本电脑上使用带有 JDK1.8.0 的 Netbeans 8,所有 x64。
我希望有人可以复制这一点,并告诉我为什么在 x Text 添加后 ChangeListener 没有执行(以及为什么 它确实在调试模式下工作)。
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
public class TextFlowTest extends Application {
final GridPane grid = new GridPane();
final Button addbtn = new Button();
final Button scrollbtn = new Button();
final TextFlow textflow = new TextFlow();
final ScrollPane textflowSP = new ScrollPane();
int counter = 0;
@Override
public void start(Stage primaryStage) {
// Attach a listener to the TextFlow component height property which
// is set after component has been rendered so we can set scroll bar to
// bottom.
DoubleProperty hProperty = new SimpleDoubleProperty();
hProperty.bind(textflow.heightProperty());
hProperty.addListener(new ChangeListener() {
@Override
public void changed(ObservableValue ov, Object t, Object t1) {
System.out.println("In listener " + ++counter + " with height of "
+ Double.toString((Double)t1));
textflowSP.setVvalue(textflowSP.getVmax());
}
}) ;
// Create Scroll Pane for TextFlow component
textflowSP.setPrefHeight(200);
textflowSP.setPrefWidth(200);
textflowSP.setFitToHeight(true);
textflowSP.setFitToWidth(true);
textflowSP.setContent(textflow);
grid.add(textflowSP,0,1);
// Button to add Text to TextFlow component
addbtn.setText("Add Text");
addbtn.setOnAction((ActionEvent e) -> {
textflow.getChildren().add(new Text("Line 1\nLine 2\nLine 3\n-\n"));
// Print shows that height property has not changed after adding Text
// but before rendering.
System.out.println(Double.toString(textflow.heightProperty().get()));
});
// Button to scoll to bottom of Scroll Pane
scrollbtn.setText("Scroll to Bottom");
scrollbtn.setOnAction((ActionEvent e) -> {
textflowSP.setVvalue(textflowSP.getVmax());
});
VBox btnVB = new VBox();
btnVB.getChildren().addAll(addbtn,scrollbtn);
grid.add(btnVB,1,1);
Scene scene = new Scene(grid, 330, 210);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
提前感谢您提供的任何帮助。
【问题讨论】:
标签: javafx changelistener