JavaFX - 无法将项目添加到表格 View

标签 java javafx

我对 JavaFX 还很陌生。我长期以来一直在绝望地尝试让它发挥作用,但不知道为什么它不起作用。这些项目没有显示在桌面 View 上。我使用场景生成器创建了 UI。我看过很多类似的问题,但似乎没有任何帮助。

简化为最少的代码:

主要:

package sample;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

Controller :

package sample;

import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import java.net.URL;
import java.util.ResourceBundle;


public class Controller implements Initializable {
    ObservableList<Person> people = FXCollections.observableArrayList();
    @FXML private TableView<Person> table;
    @FXML private TableColumn<Person, SimpleStringProperty> c1;
    @FXML private TableColumn<Person, SimpleStringProperty> c2;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        c1.setCellValueFactory(new PropertyValueFactory<>("age"));
        c2.setCellValueFactory(new PropertyValueFactory<>("name"));

        people.add(new Person("2", "Sam"));
        people.add(new Person("1", "Met"));
        table.setItems(people);
    }


}

人员类别:

package sample;

import javafx.beans.property.SimpleStringProperty;

public class Person {
    private SimpleStringProperty age;
    private SimpleStringProperty name;


    public Person(String age, String name) {
        this.age = new SimpleStringProperty(age);
        this.name = new SimpleStringProperty(name);
    }
}

FXML:

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

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>


<?import javafx.scene.control.cell.PropertyValueFactory?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
            minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1"
        xmlns:fx="http://javafx.com/fxml/1">
  <TableView fx:id="table" layoutX="51.0" layoutY="31.0" prefHeight="338.0" prefWidth="498.0">
        <columns>
          <TableColumn fx:id="c1" prefWidth="75.0" text="C1">
            <cellValueFactory>
                <PropertyValueFactory property="c1" />
            </cellValueFactory>
          </TableColumn>
            <TableColumn fx:id="c2" prefWidth="75.0" text="C1">
                <cellValueFactory>
                    <PropertyValueFactory property="c2" />
                </cellValueFactory>
            </TableColumn>
        </columns>
      </TableView>
</AnchorPane>

谢谢。

最佳答案

要使其发挥作用,您需要进行一些更改。阅读代码中的注释:

Person 类必须具有公共(public) getter:

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Person {

    private final SimpleStringProperty age;
    private final SimpleStringProperty name;

    public Person(String age, String name) {
        this.age = new SimpleStringProperty(age);
        this.name = new SimpleStringProperty(name);
    }

    //add getters to properties with the appropriate naming convention  
    public final StringProperty ageProperty() {
        return age;
    }

    public final StringProperty nameProperty() {
        return name;
    }
}  

在 fxml 中,您必须指定 Controller 。另请注意 TableView 中的更改:

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

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<?import javafx.scene.control.cell.PropertyValueFactory?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
            minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1"
           xmlns:fx="http://javafx.com/fxml/1"  
           fx:controller="xml_tabel_1.Controller"> <!-- need to specify the controller of the fxml -->
  <TableView items="${controller.people}"> <!-- need to specify the name of the observable list -->
        <columns>
          <TableColumn prefWidth="75.0" text="age">

            <cellValueFactory>
                <PropertyValueFactory property="age" />
            </cellValueFactory>
          </TableColumn>
            <TableColumn prefWidth="75.0" text="name">
                <cellValueFactory>
                    <PropertyValueFactory property="name" />
                </cellValueFactory>
            </TableColumn>
        </columns>
      </TableView>
</AnchorPane>

在 Controller 中您需要做的就是填充可观察列表并为其提供一个公共(public) getter :

import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.Initializable;

public class Controller implements Initializable {

    private final ObservableList<Person> people = FXCollections.observableArrayList();
    //all this was already set in fxml 
    //@FXML private TableView<Person> table;
    //@FXML private TableColumn<Person, SimpleStringProperty> c1;
    //@FXML private TableColumn<Person, SimpleStringProperty> c2;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        people.add(new Person("2", "Sam"));
        people.add(new Person("1", "Met"));
    }

    //add a getter to people 
    public ObservableList<Person> getPeople() {
        return people ;
    }
}

关于JavaFX - 无法将项目添加到表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56208579/

相关文章:

java - 使用 JavaFX 将图像拖到窗口外

java - 在 JavaFX + Clojure 中加载自定义字体

java - Jersey 2.4 ReaderInterceptor 不工作

java - 未能评估断点条件。原因 : Object has been collected

java.lang.UnsatisfiedLinkError : Native Library: {. ..}.dll 已经加载到另一个类加载器中

JavaFX 在线程任务完成后显示对话

Java,如何在 JavaFX 中暂停

java - 如何使用可序列化对象和回调方法正确模拟 Java RMI 中的按引用传递?

JavaFX - 由 : java. lang.UnsupportedOperationException : Unable to open DISPLAY? 引起

JavaFX 重叠场景