ListView没有显示应用程序中的项目.为什么不? [英] ListView did not show items in app. Why not?

查看:81
本文介绍了ListView没有显示应用程序中的项目.为什么不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I got items on my listview roles but they are not showing in my program after running in window. Gui done in Scene Builder. When i run program field where should be items is white blank just empty.

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

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

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

When i use show sample data in Scene Builder it shows lorem ipsum etc.

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

here u got code from controller and fmxl document.

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

}

fxml

<?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>

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

initData ControllerNewGame.java startGame() is called when is activaded button

@FXML
    void startGame() {
        try{
            Parent root= FXMLLoader.load(getClass().getResource("BindColor.fxml"));

            ControllerBindColors controller = new ControllerBindColors();
            int number = (int)slider.getValue();
            controller.initData(number,selectedRole.getText(),selectedColor.getText());
            Stage toGame = (Stage)startButton.getScene().getWindow();
            toGame.setTitle("Parasite Pip-Boy Bind Color Menu");
            toGame.setScene(new Scene(root, 600, 300));
            toGame.show();
        }catch(IOException e) {
            startButton.setText("Reinstall program. BindColor.fxml is missing or is corrupted. :(");
        }
    }

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

list has items for sure in console there are printed all 4 items [test, test2, test3, test555]

推荐答案

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

To get a reference to the controller used by the FXML (instead of constructing a new one):

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

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

after getting a reference to it, you can use it as you did:

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

这篇关于ListView没有显示应用程序中的项目.为什么不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆