AppRunner 類
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXMLLoader;
public class AppRunner {
public static void main(String[] args) {
startUi.launchPanel(args);
}
public static void waitFiveSecToClear() {
new Thread(
new Runnable() {
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(AppRunner.class.getName()).log(Level.SEVERE, null, ex);
}
FXMLLoader loader = new FXMLLoader(getClass().getResource("listView.fxml"));
try {
loader.load();
} catch (IOException ex) {
Logger.getLogger(AppRunner.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("clearing listView");
((ListViewController) loader.getController()).clearAll();
}
}).start();
}
}
ListViewController 類
package apprunner;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
/**
* FXML Controller class
*
*/
public class ListViewController implements Initializable {
public ListViewController() {
}
@FXML
private ListView<String> listView;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
listView.getItems().add("clear all!");
listView.getItems().add("clear all!");
listView.getItems().add("clear all!");
listView.getItems().add("clear all!");
AppRunner.waitFiveSecToClear();
}
public void clearAll() {
Platform.runLater(new Runnable() {
@Override
public void run() {
listView.getItems().clear();
}
});
}
}
startUi 類
package apprunner;
/**
*
*/
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class startUi extends Application {
public static void launchPanel(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("listView.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
串列視圖.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/17" fx:controller="apprunner.ListViewController">
<children>
<ListView fx:id="listView" layoutX="68.0" layoutY="39.0" prefHeight="200.0" prefWidth="200.0" />
</children>
</AnchorPane>
目前沒有錯誤。但是我的代碼沒有做我想要的。我希望 AppRunner 類中的 waitFiveSecToClear() 方法清除 javafx ui 中的 listView。然而,這并沒有發生。我曾經((ListViewController) loader.getController()).clearAll();試圖清除似乎沒有做任何事情的 listView。如果有人可以幫助我找出我的問題,那將非常感謝!
我的問題的背景
我有一個高中頂點專案。我選擇制作一個音樂互聯網下載器。當我的程式下載音樂時,它會在沒有執行緒的情況下凍結整個 javafx 應用程式,所以我在 waitFiveSecToClear() 方法中使用了一個執行緒和 Thread.sleep(5000) 來模擬它。我的程式有一個 listView,它顯示了當前正在下載的所有音樂 url 的佇列。我想要它,這樣當一個 url 完成下載時,listView 會更新,這必須在需要 Platform.runlater 的 Javafx 執行緒上完成。所以,我在 clearAll() 方法中使用了 Platform.runLater 來模擬它。我不想在控制器中這樣做,因為根據我所學到的,這會破壞模型、視圖、控制器架構。我有一個 AppRunner 課程,因為我的教師程式示例有,所以我只是復制了它。
uj5u.com熱心網友回復:
我按照@James_D 和@jewelsea 的建議使用Task 和ObservableList 解決了我的問題。我的模型中有一個任務,它將下載音樂并在完成下載后從 ObservableList 中洗掉音樂的 url。然后,我的控制器中有一個偵聽器,當 ObservableList 更改時,它將更新 ListView。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429933.html
