ListView没有显示应用程序中的项目。为什么不?

时间:2019-02-03 16:36:49

标签: javafx fxml

我在listview角色上有项目,但是在window中运行后它们没有显示在我的程序中。 Gui在Scene Builder中完成。当我运行程序字段时,项目应该是白色空白,只是空白。

程序的打印屏幕 https://zapodaj.net/c60f99a10f300.jpg.html

场景构建器 https://zapodaj.net/26fbf2123397c.jpg.html

当我在Scene Builder中使用显示示例数据时,它会显示lorem ipsum等。

在这里,您从控制器和fmxl文档中获取了代码。

if(currentUser.collegeLevel === "Associates Degree" || currentUser.collegeLevel === "High School")

fxml

package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;

import java.net.URL;
import java.util.*;

public class ControllerBindColors implements Initializable {
    @FXML
    private ListView<String > roles;
    @FXML
    private ListView<String> colors;
    @FXML
    private ListView<?> binded;
    String selectedRole;
    String selectedColor;
    int numberOfPlayers;
    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    public void initData(int numberOfPlayers, String selectedRole, String selectedColor) {
        this.numberOfPlayers = numberOfPlayers;
        this.selectedRole = selectedRole;
        this.selectedColor = selectedColor;

        ObservableList<String> itemsRoles = FXCollections.observableArrayList("test","test2","test3");
        roles = new ListView<>();
        roles.setItems(itemsRoles);
        roles.getItems().addAll("test555");
        roles.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        System.out.println(roles.getItems());
    }

}

initData 激活按钮时调用ControllerNewGame.java startGame()

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>


<VBox spacing="10.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.ControllerBindColors">
   <children>
      <HBox alignment="CENTER">
         <children>
            <ListView fx:id="roles" prefHeight="200.0" prefWidth="200.0" />
            <VBox alignment="CENTER" spacing="10.0">
               <children>
                  <Button fx:id="backBtn" ellipsisString="BACK" mnemonicParsing="false" onAction="#back" text="BACK" />
                  <Label prefHeight="51.0" prefWidth="71.0" text="Select role and color then bind." textAlignment="CENTER" wrapText="true" />
                  <Button fx:id="bindbtn" ellipsisString="BIND" mnemonicParsing="false" onAction="#bind" text="BIND" />
                  <Button fx:id="nextBtn" ellipsisString="NEXT" mnemonicParsing="false" onAction="#next" text="NEXT" />
               </children>
               <opaqueInsets>
                  <Insets />
               </opaqueInsets>
            </VBox>
            <ListView fx:id="colors" prefHeight="200.0" prefWidth="200.0" />
         </children>
      </HBox>
      <ListView fx:id="binded" prefHeight="200.0" prefWidth="200.0" />
   </children>
   <padding>
      <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
   </padding>
</VBox>

列表中有确定的项目,在控制台中,所有4个项目均已打印 [test,test2,test3,test555]

1 个答案:

答案 0 :(得分:0)

要获得对FXML使用的控制器的引用(而不是构造一个新的控制器):

FXMLLoader loader = new FXMLLoader();
Parent root= loader.load(getClass().getResource("BindColor.fxml").openStream());
ControllerBindColors controller = fxmlLoader.getController(); 

获得对它的引用后,可以像以前一样使用它:

controller.initData(number,selectedRole.getText(),selectedColor.getText());