如何在另一个类中编辑javafx ui



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();
}
}

listView.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.run Later来模拟它。我不想在控制器中这样做,因为根据我所学到的,这会破坏模型、视图和控制器架构。我有一个AppRunner类,因为我的老师的程序示例有,所以我只是复制了它

我按照@James_D和@jewelsea的建议,使用Task和ObservableList解决了我的问题。我的模型中有一个任务,它会下载音乐,并在下载完成后从ObservableList中删除音乐的url。然后,我的控制器中有一个监听器,当ObservableList发生变化时,它会更新ListView。

最新更新