AppRunner class
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 class
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 class
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>
Currently there is no error. However my code isn't doing what I want. I want the waitFiveSecToClear() method inside the AppRunner class to clear the listView in the javafx ui. However this isn't happening. I used ((ListViewController) loader.getController()).clearAll();
to attempt to clear the listView which doesen't seem to do anything. If someone could help me figure out my problem that would be great thanks!
Context for my problem
I have a highschool capstone project. I chose to make a music internet downloader. When my program is downloading music, it would freeze the whole javafx application without the thread, so I used a thread and Thread.sleep(5000) in my waitFiveSecToClear() method to simulate that. My program has a listView which shows a queue of all the music urls currently being downloaded. I want it so that when one url is finished downloading, the listView is updated which must be done on the Javafx thread which requires Platform.runlater. So, I used Platform.runLater in the clearAll() method to simluate that. I don't want to do that in the controller because based on what I've learned that would break model, view, controller architecture. I have an AppRunner class because my teachers program example had that so I just copied it.
CodePudding user response:
I solved my problem by using a Task and ObservableList as suggested by @James_D and @jewelsea. I have a Task in my model which would download music and remove the url of the music from the ObservableList once it finished downloading. I then have a Listener in my controller which would update the ListView when the ObservableList changed.