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

时间:2018-12-05 06:40:32

标签: javafx fxml scenebuilder

我只想使用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预览中实时查看。

1 个答案:

答案 0 :(得分: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="jacob.smith@example.com"/>
                    <Person firstName="Isabella" lastName="Johnson"
                            email="isabella.johnson@example.com"/>
                    <Person firstName="Ethan" lastName="Williams"
                            email="ethan.williams@example.com"/>
                    <Person firstName="Emma" lastName="Jones"
                            email="emma.jones@example.com"/>
                    <Person firstName="Michael" lastName="Brown"
                            email="michael.brown@example.com"/>
                </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="jacob.smith@example.com"/>
                    <Person firstName="Isabella" lastName="Johnson"
                            email="isabella.johnson@example.com"/>
                    <Person firstName="Ethan" lastName="Williams"
                            email="ethan.williams@example.com"/>
                    <Person firstName="Emma" lastName="Jones"
                            email="emma.jones@example.com"/>
                    <Person firstName="Michael" lastName="Brown"
                            email="michael.brown@example.com"/>
                </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