- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我运行以下代码时,出现数组索引越界异常。我的目标是绑定(bind) Text 对象和 ProgressBar 对象,以便它们同时更新。我尝试使用双向绑定(bind)。我一定是踩到什么地方了。我已经运行了它很多次,并且已经看到它成功运行了一两次。通常,在观察进度条一段时间后,就会发生异常。有时我看到一个或两个文本对象在各自的进度条上停止更新,但进度条继续前进,直到出现异常。 JavaFX 绑定(bind)专家请帮忙。
Exception in thread "JavaFX Application Thread" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(ArrayList.java:418)
at java.util.ArrayList.get(ArrayList.java:431)
at javafx.scene.Parent.updateCachedBounds(Parent.java:1580)
at javafx.scene.Parent.recomputeBounds(Parent.java:1524)
at javafx.scene.Parent.impl_computeGeomBounds(Parent.java:1377)
at javafx.scene.Node.updateGeomBounds(Node.java:3556)
at javafx.scene.Node.getGeomBounds(Node.java:3509)
at javafx.scene.Node.getLocalBounds(Node.java:3457)
at javafx.scene.Node.updateTxBounds(Node.java:3620)
at javafx.scene.Node.getTransformedBounds(Node.java:3403)
at javafx.scene.Node.updateBounds(Node.java:538)
at javafx.scene.Parent.updateBounds(Parent.java:1708)
at javafx.scene.Parent.updateBounds(Parent.java:1706)
at javafx.scene.Parent.updateBounds(Parent.java:1706)
at javafx.scene.Parent.updateBounds(Parent.java:1706)
at javafx.scene.Parent.updateBounds(Parent.java:1706)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2404)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:314)
at com.sun.javafx.tk.Toolkit$$Lambda$163/2039336538.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:313)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:340)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:525)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:505)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$400(QuantumToolkit.java:334)
at com.sun.javafx.tk.quantum.QuantumToolkit$$Lambda$40/1271950976.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
这是完整的源代码。
package javafxapplication2;
import java.io.BufferedReader;
import java.net.URL;
import java.security.Provider.Service;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextBoundsType;
import javafx.util.StringConverter;
import javafx.util.converter.DoubleStringConverter;
import javafx.util.converter.NumberStringConverter;
import org.controlsfx.control.HyperlinkLabel;
public class FXMLDocumentController implements Initializable {
@FXML
private TreeView treeView;
List<JobProgress> jobProgressInfos = Arrays.<JobProgress>asList(
new JobProgress("JJ&P_POP_DAT", "Running Jobs"),
new JobProgress("Des_master_33334", "Running Jobs"),
new JobProgress("FS_BUSS", "Running Jobs"),
new JobProgress("CONER_DWN_MIX", "Running Jobs"),
new JobProgress("newjb", "Running Jobs"),
new JobProgress("jb name", "Running Jobs"),
new JobProgress("tst jb", "Pending Jobs"),
new JobProgress("ult--ob", "Pending Jobs"),
new JobProgress("inl jb", "Pending Jobs"),
new JobProgress("fst jb", "Completed Jobs"),
new JobProgress("VoCt_Nme_Jb", "Completed Jobs"));
TreeItem<String> rootNode = new TreeItem<>("Job Details");
@Override
public void initialize(URL url, ResourceBundle rb) {
}
void setup()
{
rootNode.setExpanded(true);
for (JobProgress jobProgressInfo : jobProgressInfos)
{
Text text = new Text();
text.setBoundsType(TextBoundsType.VISUAL);
text.setFill(Color.BLACK);
ProgressBar bar = new ProgressBar(0.0);
//Bind bar and text:
bar.progressProperty().bind(jobProgressInfo.getJobPercentDouble());
text.textProperty().bind(jobProgressInfo.getJobPercentString());
StackPane stack = new StackPane();
stack.getChildren().addAll(bar,text);
Node icon = stack;
text.textProperty().addListener(new ChangeListener<String>()
{
public void changed(ObservableValue ov, String t, String t1) {
System.out.println(ov);
System.out.println(t);
System.out.println(t1);
System.out.println("");
}
});
TreeItem<String> empLeaf = new TreeItem<>(jobProgressInfo.getJobName(),icon);
boolean found = false;
for (TreeItem<String> depNode : rootNode.getChildren())
{
if (depNode.getValue().contentEquals(jobProgressInfo.getJobStatus()))
{
depNode.getChildren().add(empLeaf);
found = true;
break;
}
}
if (!found) {
TreeItem<String> depNode = new TreeItem<>(
jobProgressInfo.getJobStatus()
);
rootNode.getChildren().add(depNode);
depNode.getChildren().add(empLeaf);
}
}
treeView.setRoot(rootNode);
treeView.addEventHandler(MouseEvent.MOUSE_CLICKED,new EventHandler<MouseEvent>()
{
@Override public void handle(MouseEvent mouseEvent)
{
if ( mouseEvent.getButton().equals(MouseButton.PRIMARY))
{
if (treeView.getSelectionModel().getSelectedItem() instanceof TreeItem)
{
TreeItem item = (TreeItem)treeView.getSelectionModel().getSelectedItem();
if (item.getParent() != null && item.getParent().getParent() != null)
{
System.out.println("You primary clicked a job!");
}
}
}
else if ( mouseEvent.getButton().equals(MouseButton.SECONDARY))
{
if (treeView.getSelectionModel().getSelectedItem() instanceof TreeItem)
{
TreeItem item = (TreeItem)treeView.getSelectionModel().getSelectedItem();
if (item.getParent() != null && item.getParent().getParent() != null)
{
System.out.println("You scondary clicked a job!");
}
}
}
}
});
Service svc = new Service();
svc.createTask().run();
}
public class Service extends javafx.concurrent.Service<Void>
{
@Override
protected Task<Void> createTask()
{
return new TestTask();
}
private class TestTask extends Task<Void>
{
public TestTask()
{
}
@Override
protected Void call() throws Exception
{
for (JobProgress jp : jobProgressInfos)
{
new Thread(){
@Override
public void run(){
try {
for(double i=0; i<=1; i+=0.01)
{
DecimalFormat df = new DecimalFormat("#.####");
jp.setJobPercentDouble(Double.parseDouble( df.format(i)));
Thread.sleep(500);
}
} catch (InterruptedException ex) {
System.err.println("Error on Thread Sleep");
}
}
}.start();
}
return null;
}
@Override
protected void succeeded()
{
}
@Override
protected void cancelled()
{
}
}
}
public static class JobProgress
{
private final SimpleDoubleProperty jobPercentDouble;
private final SimpleStringProperty jobPercentString;
private final SimpleStringProperty jobName;
private final SimpleStringProperty jobStatus;
private final HyperlinkLabel hyperLink;
private JobProgress(String aJobName, String aJobStatus) {
this.jobName = new SimpleStringProperty(aJobName);
this.jobStatus = new SimpleStringProperty(aJobStatus);
this.hyperLink = new HyperlinkLabel("test");
this.jobPercentDouble = new SimpleDoubleProperty();
this.jobPercentString = new SimpleStringProperty();
Bindings.bindBidirectional(jobPercentString,jobPercentDouble, new NumberStringConverter());
// StringConverter<? extends Number> converter = new NumberStringConverter();
// Bindings.bindBidirectional(stringProp, doubleProp, new Format()
// {
//
// @Override
// public StringBuffer format(Object obj, StringBuffer toAppendTo,FieldPosition pos)
// {
// System.out.println(obj.toString());
// return toAppendTo.append(obj.toString());
// }
//
// @Override
// public Object parseObject(String source, ParsePosition pos)
// {
// System.out.println("parseObj " + source);
// return Integer.parseInt(source);
// }
//
// });
}
public String getJobName() {
return jobName.get();
}
public void setJobName(String value) {
jobName.set(value);
}
public String getJobStatus() {
return jobStatus.get();
}
public void setJobStatus(String value) {
jobStatus.set(value);
}
public SimpleStringProperty getJobPercentString() {
return jobPercentString;
}
public void setJobPercentString(String value) {
jobPercentString.set(value);
}
public SimpleDoubleProperty getJobPercentDouble() {
return jobPercentDouble;
}
public void setJobPercentDouble(double value) {
jobPercentDouble.set(value);
}
}
}
这是 FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="509.0" prefWidth="222.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="javafxapplication2.FXMLDocumentController">
<children>
<TreeView fx:id="treeView" layoutX="11.0" layoutY="5.0" prefHeight="496.0" prefWidth="200.0" AnchorPane.bottomAnchor="8.0" AnchorPane.leftAnchor="11.0" AnchorPane.rightAnchor="11.0" AnchorPane.topAnchor="5.0" />
</children>
</AnchorPane>
最后但并非最不重要的一点是:
package javafxapplication2;
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXApplication2 extends Application {
@Override
public void start(Stage stage) throws Exception
{
URL resource = getClass().getResource("FXMLDocument.fxml");
FXMLLoader loader = new FXMLLoader(resource);
Parent root = (Parent) loader.load();
FXMLDocumentController controller = loader.getController();
controller.setup();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
最佳答案
您将如此多的部分混合在一起,例如服务、任务、线程。而且您的命名约定与 JavaFX 的命名约定不同,就像您的属性一样。
在我稍微清理了代码并制作了自己的 JobProgress 类文件之后,您现在应该准备好获取它了。
您错过了在 JobProgress 对象上设置初始进度。那么如果不存在数字,双向绑定(bind)应该如何将数字转换为字符串?!
此外,我还删除了一些监听器和操作处理程序。您应该能够简单地再次添加它们。
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextBoundsType;
public class FXMLDocumentController implements Initializable {
@FXML
private TreeView treeView;
private final List<JobProgress> jobList = Arrays.<JobProgress>asList(
new JobProgress("JJ&P_POP_DAT", "Running Jobs"),
new JobProgress("Des_master_33334", "Running Jobs"),
new JobProgress("FS_BUSS", "Running Jobs"),
new JobProgress("CONER_DWN_MIX", "Running Jobs"),
new JobProgress("newjb", "Running Jobs"),
new JobProgress("jb name", "Running Jobs"),
new JobProgress("tst jb", "Pending Jobs"),
new JobProgress("ult--ob", "Pending Jobs"),
new JobProgress("inl jb", "Pending Jobs"),
new JobProgress("fst jb", "Completed Jobs"),
new JobProgress("VoCt_Nme_Jb", "Completed Jobs"));
@Override
public void initialize(URL url, ResourceBundle rb) {
TreeItem<String> rootNode = new TreeItem<>("Job Details");
rootNode.setExpanded(true);
for (JobProgress jb : jobList) {
Text text = new Text();
ProgressBar bar = new ProgressBar(0.0);
bar.progressProperty().bind(jb.jobProgressProperty());
text.setBoundsType(TextBoundsType.VISUAL);
text.setFill(Color.BLACK);
text.textProperty().bind(jb.jobProgressStringProperty());
StackPane stack = new StackPane();
stack.getChildren().addAll(bar, text);
Node icon = stack;
TreeItem<String> empLeaf = new TreeItem<>(jb.getJobName(), icon);
boolean found = false;
for (TreeItem<String> depNode : rootNode.getChildren()) {
if (depNode.getValue().contentEquals(jb.getJobStatus())) {
depNode.getChildren().add(empLeaf);
found = true;
break;
}
}
if (!found) {
TreeItem<String> depNode = new TreeItem<>(jb.getJobStatus());
rootNode.getChildren().add(depNode);
depNode.getChildren().add(empLeaf);
}
}
treeView.setRoot(rootNode);
new Thread(simulateProgress()).start();
}
private Task<Void> simulateProgress() {
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
for (double i = 0.0; i <= 1.0; i += 0.1) {
for (JobProgress job : jobList) {
job.setJobProgress(i);
}
Thread.sleep(1000);
}
return null;
}
};
return task;
}
}
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXApplication2 extends Application {
@Override
public void start(Stage stage) throws Exception {
URL resource = getClass().getResource("FXMLDocument.fxml");
FXMLLoader loader = new FXMLLoader(resource);
Parent root = (Parent) loader.load();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.util.converter.NumberStringConverter;
public class JobProgress {
private final SimpleDoubleProperty jobProgress;
public double getJobProgress() {
return jobProgress.get();
}
public void setJobProgress(double value) {
jobProgress.set(value);
}
public SimpleDoubleProperty jobProgressProperty() {
return jobProgress;
}
private final SimpleStringProperty jobProgressString;
public void setJobProgressString(String value) {
jobProgressString.set(value);
}
public String getJobProgressString() {
return jobProgressString.get();
}
public SimpleStringProperty jobProgressStringProperty() {
return jobProgressString;
}
private final SimpleStringProperty jobName;
public String getJobStatus() {
return jobStatus.get();
}
public void setJobStatus(String value) {
jobStatus.set(value);
}
private final SimpleStringProperty jobStatus;
public String getJobName() {
return jobName.get();
}
public void setJobName(String value) {
jobName.set(value);
}
public JobProgress(String jobName, String jobStatus) {
this.jobName = new SimpleStringProperty(jobName);
this.jobStatus = new SimpleStringProperty(jobStatus);
this.jobProgress = new SimpleDoubleProperty(0.0);
this.jobProgressString = new SimpleStringProperty();
Bindings.bindBidirectional(jobProgressString, jobProgress, new NumberStringConverter());
}
}
解决方案如下所示:
关于Javafx双向绑定(bind)SimpleDoubleProperty SimpleStringProperty数组越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31035081/
我有 2 个类:User 和 UserPicture,它们具有 1:1 关系。 public class User { @Id @GeneratedValue(strategy=G
使用ssh转发时,我无法针对远程服务器使用cvs和ftp进行提交。是否可以让服务器对我的机器发起请求-我希望服务器上的Web应用程序调用我的机器上的REST方法。 谢谢。 尼古拉·G。 最佳答案 是的
我正在 Python 2.7.12 中实现双向 A* 算法,并在 Russell 和 Norvig 第 3 章的罗马尼亚 map 上进行测试。边具有权重,目的是找到两个节点之间的最短路径。 这是测试图
您能否建议一种映射或类似的数据结构,让我们可以轻松地相互获取值和键。也就是说,每个都可以用来寻找另一个。 最佳答案 Java 在其标准库中没有双向映射。 例如使用 BiMap 来自Google Gua
我想同步两个数据库运行时 服务器 A:安装了公共(public) IP 和 mysql 的 Amazon ec2。服务器B:这是局域网中带有mysql的私有(private)机器。 (IP是私有(pr
保存双向@OneToOne 映射时,hibernate 是否应该在两个表上都记录? 我有一个包含 applicant_id 列的表 interview,它引用了包含字段 interview_id 的
我喜欢新的 SwipeRefreshLayout!它看起来很棒,而且非常容易使用。但我想在两个方向上使用它。我有一个消息屏幕,我想通过从上到下滑动来加载旧消息,我想通过从下到上滑动来加载新消息。 这个
使用 ICS 4.0.1(愿意升级到 4.0.3)(不会 root 和重写 android 操作系统) 在接收到 android beam 后,是否可以将 NDEF 消息发送回 android 手机
我想知道处理这种 git 场景的最佳方法: Git 仓库:CoreProduct Git repo b: SpecificCustomerProduct 是从 a fork 出来的 到目前为止,我们一
这个问题在这里已经有了答案: How to implement an efficient bidirectional hash table? (8 个回答) 关闭2年前。 我在 python 中做这个
您能否推荐一种 map 或类似的数据结构,我们可以在其中轻松地从彼此获取值和键。也就是说,每个都可以用来寻找另一个。 最佳答案 Java 在其标准库中没有双向映射。 例如使用 BiMap 来自 Goo
Java中是否有类似双面列表的东西?也许第三方实现? 这里有一个小例子来证明我的想法。 原始状态: 答:0-1-2-3 | | | | 乙:0-1-2-3 删除 B 中的元素 1 后: 空值 | 答:
我有两个实体通过这样的双向 OneToOne 关联连接: @Entity class Parent { @NotNull String businessKey; @OneToO
我已将 Vagrant 配置为使用 Rsync 共享文件夹而不是(非常慢)vboxsf VirtualBox 默认提供的文件系统: Vagrant.configure("2") do |config|
@keyframes mgm { from { max-height: 250px; } to { max-height: 0px; } } .mgm {
我想了解有关使用双向 LSTM 进行序列分类时合并模式的更多详细信息,尤其是对于我还不清楚的“Concat”合并模式。 根据我对这个方案的理解: 在将前向和后向层的合并结果传递到 sigmoid 函数
我有兴趣将本地 git 存储库设置为远程存储库的镜像。我已经阅读了一些可能相关的帖子,但主要区别在于我需要对两个存储库进行读写访问。 大多数时候,用户会针对 Repo A 工作,但是有时他们会针对 R
我已经仔细阅读了文档 https://firebase.google.com/docs/database/web/read-and-write以及网上很多例子。但这里有一个脱节:在将对象添加到数据库时
这个问题已经有答案了: Hibernate bidirectional @ManyToOne, updating the not owning side not working (3 个回答) 已关闭
我知道有很多关于它的问题,但我找不到针对我的问题的好的答案。 我使用 Jboss 作为 7,Spring 和 Hibernate (4) 作为 JPA 2.0 提供程序,因此我有简单的 @OneToM
我是一名优秀的程序员,十分优秀!