首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在另一个类中编辑javafx?

如何在另一个类中编辑javafx?
EN

Stack Overflow用户
提问于 2022-02-05 23:53:52
回答 1查看 107关注 0票数 1

AppRunner类

代码语言:javascript
运行
复制
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类

代码语言:javascript
运行
复制
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类

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
<?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>

目前没有错误。然而,我的代码并没有做我想做的事情。我希望waitFiveSecToClear()方法在AppRunner类中清除javafx中的listView。然而,这种情况并没有发生。我使用((ListViewController) loader.getController()).clearAll();试图清除似乎什么都不做的listView。如果有人能帮我解决我的问题,那就太好了!

我的问题的背景

我有个高中帽石项目。我选择了制作一个音乐网络下载器。当我的程序下载音乐时,它会在没有线程的情况下冻结整个javafx应用程序,所以我在我的waitFiveSecToClear()方法中使用了一个线程和Thread.sleep(5000)来模拟这个过程。我的程序有一个listView,它显示当前正在下载的所有音乐urls的队列。我希望这样,当下载完一个url时,listView就会被更新,这必须在需要Platform.runlater的Javafx线程上完成。因此,我在clearAll()方法中使用了clearAll来简化这个过程。我不想在控制器中这样做,因为根据我所学到的,它会破坏模型、视图、控制器体系结构。我有一个AppRunner课程,因为我的老师程序的例子有,所以我只是复制它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-06 19:33:29

我通过使用任务和ObservableList来解决我的问题,就像@James_D和@珠宝油建议的那样。我在我的模型中有一个任务,它将下载音乐,并在完成下载后从ObservableList中删除音乐的url。然后,我的控制器中有一个侦听器,它将在ListView更改时更新ObservableList。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71003130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档