【问题标题】:ListView items do not appear in JavaFx appListView 项目不会出现在 JavaFx 应用程序中
【发布时间】:2017-04-18 15:44:45
【问题描述】:

由于某种原因,列表中的文本未填充指定的 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>

【问题讨论】:

    标签: java listview javafx


    【解决方案1】:

    您尚未在 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替换为控制器类的实际包名)。

    【讨论】:

    • 我应该将它附加到我的 AnchorPane 中吗?或者只是在我的 ListView 周围添加一个新的窗格?
    • @santafebound 不,您只需将缺少的 &lt;fx:controller&gt; 属性添加到现有的 &lt;Pane&gt; 根元素中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多