java - 缺少显示表内数据的代码

标签 java netbeans javafx

目标:
在表格内部下方显示数据

"Jacob", "Smith", "jacob.smith@example.com"
"Isabella", "Johnson", "isabella.johnson@example.com"
"Ethan", "Williams", "ethan.williams@example.com"
"Emma", "Jones", "emma.jones@example.com"
"Michael", "Brown", "michael.brown@example.com"

问题:
为了显示表内的数据,我缺少什么代码?

信息:

  • 我是 JavaFX 新手。
  • 我正在使用 netbeans
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="787.0" prefWidth="1086.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <MenuBar BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
   <center>
      <TableView id="ttt" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <columns>
          <TableColumn id="firstNameCol" prefWidth="124.0" text="First name" />
          <TableColumn id="lastNameCol" prefWidth="139.0" text="Last name" />
            <TableColumn id="emailCol" minWidth="7.0" prefWidth="197.0" text="Email" />
            <TableColumn id="addressCol" prefWidth="105.0" text="Address" />
            <TableColumn id="zipcodeCol" prefWidth="100.0" text="Zipcode" />
            <TableColumn id="cityCol" prefWidth="204.0" text="City" />
        </columns>
      </TableView>
   </center>
</BorderPane>
package dreamcrm;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class DreamCRM extends Application {

    private TableView<Person> table = new TableView<Person>();
    private final ObservableList<Person> data =
        FXCollections.observableArrayList(
            new Person("Jacob", "Smith", "jacob.smith@example.com"),
            new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
            new Person("Ethan", "Williams", "ethan.williams@example.com"),
            new Person("Emma", "Jones", "emma.jones@example.com"),
            new Person("Michael", "Brown", "michael.brown@example.com")
        );    


    @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);
    }

}
package dreamcrm;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;


public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

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

}
package dreamcrm;

import javafx.beans.property.SimpleStringProperty;


public class Person {

    private final SimpleStringProperty firstName;
    private final SimpleStringProperty lastName;
    private final SimpleStringProperty email;

    public Person(String fName, String lName, String email) {
        this.firstName = new SimpleStringProperty(fName);
        this.lastName = new SimpleStringProperty(lName);
        this.email = new SimpleStringProperty(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);
    }    

}

最佳答案

为此,您需要使用 fx:id 而不是 idfx:id 仍然适用于 CSS 选择器,所以不用担心。

正如 @fabian 指出的,我错过了,您还需要在 fxml 文件中添加 fx:controller ,以便它知道什么将控制它。

在您的文档 Controller 中,您需要让它知道它正在使用 @FXML 从您的 fxml 文档中获取一些信息

就这样,

@FXML
private TableView ttt;
@FXML
private TableColumn<Person, String> firstNameCol, lastNameCol, emailCol,addressCol, zipCol, cityCol;

在这里,您使用 fx:id 作为变量名称。

然后,在您的 initialize 函数中,您需要设置 cellValueFactory,如下所示。

@FXML
public void initialize(){
    firstNamecol.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
//etc..

//Don't forget to add your list that you made to the tableview
ttt.setItems(list);
}

添加 ObservableList 后,您现在可以添加和删除项目,并且它应该相应更新。

在相关说明中,您需要将 SimpleStringProperty 作为可返回值,即

public StringProperty firstNameProperty(){
    return firstName;
}

这应该可以帮助您入门。

关于java - 缺少显示表内数据的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40002308/

相关文章:

java - Java FX 中的鼠标滚动

javafx - 任何像 recyclerview 或 javafx 的可重用 View 之类的东西

java - 当为我的类调用隐式 toString() 时,我可以列出所有情况吗?

java - 设置超时时套接字不关闭

java - 报告了错误的 OpenGL 版本

java - 将 log4j.xml 包含在项目 Jar 中

java - ANDROID STUDIO-布局背景(图像)不显示

java - ubuntu 上的 Netbeans 主动打包 - 找不到 dpkg

netbeans - 更改 NetBeans 中的语法突出显示

java - TableView JavaFX 中的上下文菜单可见性