ListView项目不会出现在JavaFx应用程序中

时间:2017-04-18 15:44:46

标签: java listview javafx

由于某种原因,列表中的文本没有填充指定的ListView。字符串列表应显示在左上角的网格窗格中。我该怎么办?

主要课程:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        // Select main layout file
        Parent root = FXMLLoader.load(getClass().getResource("/resources/static/fxml/scene.fxml"));

        // Set Scene window params
        primaryStage.setTitle("React");
        primaryStage.setScene(new Scene(root, 1150, 600));
        primaryStage.setResizable(false);

        // Set task bar primary icon
        primaryStage.getIcons().add(new javafx.scene.image.Image("/resources/static/images/react-app-icon.png"));

        // Show Scene
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

ListView控制器:

public class ListViewController implements Initializable {

    @FXML private ListView<String> listView1;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ObservableList<String> data = FXCollections.observableArrayList(
                "Ticket 1","Ticket 2","Ticket 3","Ticket 4"
        );
        listView1.setItems(data);
    }
}

场景:

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

<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>

<Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <GridPane layoutX="63.0" layoutY="72.0" prefHeight="500.0" prefWidth="1072.0">
            <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            </columnConstraints>
            <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            </rowConstraints>
            <children>
                <ScrollPane prefHeight="200.0" prefWidth="200.0" vbarPolicy="ALWAYS" GridPane.columnIndex="1" GridPane.rowIndex="1">
                    <content>
                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="250.0" prefWidth="520.0">
                            <children>
                                <ListView prefHeight="250.0" prefWidth="520.0" style="-fx-background-color: #FFE0B2;" /> <!-- Orange -->
                            </children>
                        </AnchorPane>
                    </content>
                </ScrollPane>
                <ScrollPane prefHeight="250.0" prefWidth="520.0" vbarPolicy="ALWAYS">
                    <content>
                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="250.0" prefWidth="520.0">
                            <children>
                                <ListView prefHeight="250.0" prefWidth="520.0" style="-fx-background-color: #C8E6C9;" fx:id="listView1" /> <!-- Green -->
                            </children>
                        </AnchorPane>
                    </content>
                </ScrollPane>
                <ScrollPane prefHeight="200.0" prefWidth="200.0" vbarPolicy="ALWAYS" GridPane.columnIndex="1">
                    <content>
                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="250.0" prefWidth="520.0">
                            <children>
                                <ListView prefHeight="250.0" prefWidth="520.0" style="-fx-background-color: #FFCCBC;" /> <!-- Red -->
                            </children>
                        </AnchorPane>
                    </content>
                </ScrollPane>
                <ScrollPane prefHeight="200.0" prefWidth="200.0" vbarPolicy="ALWAYS" GridPane.rowIndex="1">
                    <content>
                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="250.0" prefWidth="520.0">
                            <children>
                                <ListView prefHeight="250.0" prefWidth="520.0" style="-fx-background-color: #FFF9C4;" /> <!-- Yellow -->
                            </children>
                        </AnchorPane>
                    </content>
                </ScrollPane>
            </children>
        </GridPane>
        <ToolBar maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="35.0" prefWidth="1200.0" style="-fx-background-color: #FF5001;">
            <items>
                <ImageView fitHeight="35.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
                    <image>
                        <Image url="@../images/react-toolbar-logo.png" />
                    </image>
                </ImageView>
            </items>
        </ToolBar>
    </children>
</Pane>

1 个答案:

答案 0 :(得分:3)

您尚未在FXML文件中指定控制器类。你需要

<Pane fx:controller="my.package.ListViewController" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1">

<!-- ... -->

</Pane>

(将my.package替换为控制器类的实际包名称。)

相关问题