如何仅使用fxml(没有任何java代码)在表视图中添加数据?

问题描述 投票:-1回答:1

我想仅使用fxml在表视图中添加测试数据。我不知道怎么样!

像这样。

例如,我使用fxml在组合框中插入数据。像这样。

enter image description here

它仅使用fxml生成。例如

<ComboBox prefHeight="25.0" prefWidth="193.0" promptText="Select your best language" visibleRowCount="5">
    <items>
        <FXCollections fx:factory="observableArrayList">
            <String fx:value="java" />
            <String fx:value="javafx" />
            <String fx:value="css" />
            <String fx:value="fxml" />
            <String fx:value="c++" />
        </FXCollections>
    </items>
</ComboBox>

我还使用fxml添加列表视图。像这样。

enter image description here

使用fxml:

<ListView maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="200.0" prefWidth="200.0" >
   <items>
        <FXCollections fx:factory="observableArrayList">
            <String fx:value="java" />
            <String fx:value="javafx" />
            <String fx:value="css" />
            <String fx:value="fxml" />
            <String fx:value="c++" />
            <String fx:value="visual basic" />
            <String fx:value="groovy" />
            <String fx:value="coltion" />
        </FXCollections>
    </items>
</ListView>

但是如何在table-view中添加数据。仅使用fxml。我想要这个,以便我可以在scene-builder预览中查看实时。

javafx fxml scenebuilder
1个回答
0
投票

这是一个例子。来自here的代码。

关键代码

        <TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="1">
            <columns>
                <TableColumn text="First Name">
                    <cellValueFactory>
                        <PropertyValueFactory property="firstName" />
                    </cellValueFactory>
                </TableColumn>
                <TableColumn text="Last Name">
                    <cellValueFactory>
                        <PropertyValueFactory property="lastName" />
                    </cellValueFactory>
                </TableColumn>
                <TableColumn text="Email Address">
                    <cellValueFactory>
                        <PropertyValueFactory property="email" />
                    </cellValueFactory>
                </TableColumn>
            </columns>
            <items>
                <FXCollections fx:factory="observableArrayList">
                    <Person firstName="Jacob" lastName="Smith"
                            email="[email protected]"/>
                    <Person firstName="Isabella" lastName="Johnson"
                            email="[email protected]"/>
                    <Person firstName="Ethan" lastName="Williams"
                            email="[email protected]"/>
                    <Person firstName="Emma" lastName="Jones"
                            email="[email protected]"/>
                    <Person firstName="Michael" lastName="Brown"
                            email="[email protected]"/>
                </FXCollections>
            </items>
        </TableView>

完整代码:

主要

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication312 extends Application
{

    @Override
    public void start(Stage stage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

FXML

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.collections.*?>
<?import javafxapplication312.Person?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication312.FXMLDocumentController">
    <children>
        <TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="1">
            <columns>
                <TableColumn text="First Name">
                    <cellValueFactory>
                        <PropertyValueFactory property="firstName" />
                    </cellValueFactory>
                </TableColumn>
                <TableColumn text="Last Name">
                    <cellValueFactory>
                        <PropertyValueFactory property="lastName" />
                    </cellValueFactory>
                </TableColumn>
                <TableColumn text="Email Address">
                    <cellValueFactory>
                        <PropertyValueFactory property="email" />
                    </cellValueFactory>
                </TableColumn>
            </columns>
            <items>
                <FXCollections fx:factory="observableArrayList">
                    <Person firstName="Jacob" lastName="Smith"
                            email="[email protected]"/>
                    <Person firstName="Isabella" lastName="Johnson"
                            email="[email protected]"/>
                    <Person firstName="Ethan" lastName="Williams"
                            email="[email protected]"/>
                    <Person firstName="Emma" lastName="Jones"
                            email="[email protected]"/>
                    <Person firstName="Michael" lastName="Brown"
                            email="[email protected]"/>
                </FXCollections>
            </items>
        </TableView>
    </children>
</AnchorPane>

调节器

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;

/**
 *
 * @author blj0011
 */
public class FXMLDocumentController implements Initializable
{

    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        // TODO
    }

}

import javafx.beans.property.SimpleStringProperty;

public class Person
{

    private final SimpleStringProperty firstName = new SimpleStringProperty("");
    private final SimpleStringProperty lastName = new SimpleStringProperty("");
    private final SimpleStringProperty email = new SimpleStringProperty("");

    public Person()
    {
        this("", "", "");
    }

    public Person(String firstName, String lastName, String email)
    {
        setFirstName(firstName);
        setLastName(lastName);
        setEmail(email);
    }

    public String getFirstName()
    {
        return firstName.get();
    }

    public void setFirstName(String fName)
    {
        firstName.set(fName);
    }

    public String getLastName()
    {
        return lastName.get();
    }

    public void setLastName(String fName)
    {
        lastName.set(fName);
    }

    public String getEmail()
    {
        return email.get();
    }

    public void setEmail(String fName)
    {
        email.set(fName);
    }
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.