使用非Controller类中的非@FXML函数更新ListView

时间:2019-06-09 23:06:39

标签: java listview javafx

我有一个JavaFX Application和ListView。我想基于不是控制器的另一个类的事件来更新此ListView(此类与JavaFX无关)。如果我调用自己的书面方法,它将执行诸如System.out.println(“ Debug”)之类的常规代码,但不会执行AnListView.getItems().add("Pork"); 所以我的问题是如何使它还执行列表中项目的更新!

在ButtonClickEvent中进行更新时,它工作得很好。

package gui.controller;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import main.Main;
import utils.Player;

public class ListViewController implements Initializable
{

    @FXML
    private ListView<String> AllPlayersList;
    private ListView<String> TeamBlueList;
    private ListView<String> TeamRedList;
    private Button Button;




    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    public void update() {


            AllPlayersList.getItems().add("Pork");
            System.out.println("FUCK");

    }

    @FXML
    private void handleButtonAction(ActionEvent event) {
        update();

    }

}

我用来调用更新函数的代码:

FXMLLoader fxmlLoader = new FXMLLoader();
                                try {
                                    Pane p = fxmlLoader.load(getClass().getResource("../gui/MainWindowFX.fxml").openStream());
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                                ListViewController fooController = (ListViewController) fxmlLoader.getController();
                                fooController.update();

我使用并打开了FXML文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="gui.controller.ListViewController">
  <!-- TODO Add Nodes -->
  <children>
    <TabPane layoutX="0.0" layoutY="0.0" prefHeight="600.0" prefWidth="800.0" tabClosingPolicy="UNAVAILABLE">
      <tabs>
        <Tab text="Teams">
          <content>
            <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="443.0" prefWidth="490.0">
              <children>
                <ScrollPane layoutX="280.0" layoutY="64.0" prefHeight="377.0" prefWidth="240.0">
                  <content>
                    <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="377.0" prefWidth="239.0">
                      <children>
                        <ListView fx:id="AllPlayersList" cache="false" layoutX="0.0" prefHeight="377.0" prefWidth="239.0" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="0.0" />
                      </children>
                    </AnchorPane>
                  </content>
                </ScrollPane>
                <ScrollPane layoutX="538.0" layoutY="65.0" prefHeight="377.0" prefWidth="240.0">
                  <content>
                    <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="377.0" prefWidth="239.0">
                      <children>
                        <ListView id="TeamBlueList" cache="false" layoutX="0.0" prefHeight="377.0" prefWidth="239.0" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="0.0" />
                      </children>
                    </AnchorPane>
                  </content>
                </ScrollPane>
                <ScrollPane layoutX="25.0" layoutY="64.0" prefHeight="377.0" prefWidth="240.0">
                  <content>
                    <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="377.0" prefWidth="239.0">
                      <children>
                        <ListView fx:id="TeamRedList" cache="false" layoutX="0.0" prefHeight="377.0" prefWidth="239.0" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="0.0" />
                      </children>
                    </AnchorPane>
                  </content>
                </ScrollPane>
                <Button id="Button" layoutX="372.0" layoutY="493.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Button" />
              </children>
            </AnchorPane>
          </content>
        </Tab>
        <Tab text="Settings">
          <content>
            <AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
          </content>
        </Tab>
      </tabs>
    </TabPane>
  </children>
</AnchorPane>

我想使用update()函数更新列表。

0 个答案:

没有答案