我正在构建一个下载管理器
这里我展示了一个测试代码,它试图更新一行tableView的fileNameColumn
但是在连接到url之后它没有被更新
具体来说,此处fileName仍为hello1
,并且不会更新为hello2
。是的,是吗?
Main.java:
public static TableView<DownloadEntry> downloadsTable;
public TableColumn<DownloadEntry, String> fileNameColumn;
public void initialize(URL location, ResourceBundle resources) {
downloadsTable = new TableView<DownloadEntry>();
fileNameColumn = new TableColumn<>("File Name");
fileNameColumn.setCellValueFactory(new PropertyValueFactory<>("fileName"));
executor = Executors.newFixedThreadPool(4);
}
public void addDownloadButtonClicked() {
try{
String urlText = urlTextBox.getText();
DownloadEntry task = new DownloadEntry(new URL(urlText));
downloadsTable.getItems().add(task);
executor.execute(task);
}
catch(Exception e) {
System.out.println("addDownloadButtonClicked: " + e);
}
}
DownloadEntry.java:
public class DownloadEntry extends Task<Void> {
public SimpleStringProperty fileName;
public URL url;
//Constructor
public DownloadEntry(URL ur) throws Exception{
fileName = new SimpleStringProperty("hello");
url = ur;
}
@Override
protected Void call() {
try {
HttpURLConnection connect=(HttpURLConnection)url.openConnection();
fileName.set("hello1");
connect.connect();
fileName.set("hello2");
}
catch(Exception E) {
this.updateMessage("Error");
E.printStackTrace();
}
return null;
}
public String getFileName() {
return fileName.get();
}
public void setFileName(String fileName) {
this.fileName = new SimpleStringProperty(fileName);
}
}
请告诉您是否需要更多详情..
答案 0 :(得分:0)
您的模型未正确实施。 setFileName
方法应为
public void setFileName(String fileName) {
this.fileName.set(fileName);
}
(实现的问题是表仍然在观察旧属性,而不是您创建的新属性。)
您还需要提供一个&#34;属性访问器&#34;方法:
public StringProperty fileNameProperty() {
return fileName ;
}
这将允许表正确绑定到属性(以便它&#34;知道&#34;当其值发生变化时)。