无法填充在SceneBuilder 2中创建的JavaFX TableView

编程入门 行业动态 更新时间:2024-10-27 21:13:02
本文介绍了无法填充在SceneBuilder 2中创建的JavaFX TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试重新创建表视图示例使用场景构建器2,但无法填充我的TableView.

I am trying to re-create the table view sample using scene builder 2, but my TableView couldn’t be populated.

我在JavaFx SceneBuilder中定义了表,如下所示:

I defined my table in JavaFx SceneBuilder like so:

<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <AnchorPane id="AnchorPane" prefHeight="380.0" prefWidth="462.0" xmlns:fx="javafx/fxml/1" xmlns="javafx/javafx/8" fx:controller="tableviewsample.FXMLDocumentController"> <children> <Button fx:id="button" layoutX="325.0" layoutY="324.0" onAction="#handleButtonAction" text="Click Me!" /> <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" /> <TableView fx:id="table" layoutX="26.0" layoutY="29.0" prefHeight="221.0" prefWidth="411.0"> <columns> <TableColumn fx:id="firstNameCol" prefWidth="75.0" text="First Name" /> <TableColumn fx:id="lastNameCol" prefWidth="75.0" text="Last Name" /> <TableColumn fx:id="emailCol" prefWidth="75.0" text="Email" /> </columns> </TableView> <TextField fx:id="addFirstName" layoutX="26.0" layoutY="284.0" prefHeight="25.0" prefWidth="81.0" promptText="First Name" /> <TextField fx:id="addLastName" layoutX="121.0" layoutY="284.0" prefHeight="25.0" prefWidth="89.0" promptText="Last Name" /> <TextField fx:id="addEmail" layoutX="222.0" layoutY="284.0" promptText="Email" /> </children> </AnchorPane>

我的控制器代码:

package tableviewsample; import java.URL; import java.util.ResourceBundle; import javafx.beans.property.SimpleStringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Label; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextField; public class FXMLDocumentController implements Initializable { @FXML private Label label; @FXML private TableView<Person> table;// = new TableView<Person>(); @FXML private TableColumn firstNameCol ; @FXML private TableColumn lastNameCol ; @FXML private TableColumn emailCol ; @FXML TextField addFirstName; @FXML TextField addLastName; @FXML TextField addEmail; private final ObservableList<Person> data = FXCollections.observableArrayList( new Person("Jacob", "Smith", "jacob.smith@example"), new Person("Isabella", "Johnson", "isabella.johnson@example"), new Person("Ethan", "Williams", "ethan.williams@example"), new Person("Emma", "Jones", "emma.jones@example"), new Person("Michael", "Brown", "michael.brown@example") ); @FXML private void handleButtonAction(ActionEvent event) { System.out.println("You clicked me!"); data.add(new Person( addFirstName.getText(), addLastName.getText(), addEmail.getText())); addFirstName.clear(); addLastName.clear(); addEmail.clear(); } @Override public void initialize(URL url, ResourceBundle rb) { // TODO firstNameCol.setMinWidth(100); lastNameCol.setMinWidth(100); emailCol.setMinWidth(200); table.getItems().setAll(this.data); } public static class Person { private final SimpleStringProperty firstName; private final SimpleStringProperty lastName; private final SimpleStringProperty email; private 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); } } }

主要代码:

package tableviewsample; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class TableViewSample 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(); } public static void main(String[] args) { launch(args); } }

屏幕截图:

运行它时,表中有空行.如果我在3个TextField中键入一些值并单击按钮,则这些值仍未插入表中. 您能找到代码的问题吗?另外,在不相关的注释上,如何使表格可滚动?

When I run it, there are empty rows in the table. If I type some values in the 3 TextFields and click the button, still the values are not inserted in the table. Can you please find what's wrong with the code? Also, on an unrelated note, how can I make the table scrollable?

推荐答案

您尚未在TableColumn上设置任何cellValueFactory(您链接的教程).

You haven't set any cellValueFactorys on your TableColumns (Example 12-5 in the tutorial you linked).

您可以使用

<TableColumn fx:id="firstNameCol" prefWidth="75.0" text="First Name" > <cellValueFactory><PropertyValueFactory property="firstName"/></cellValueFactory> </TableColumn>

或在控制器中使用

firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));

(其他列也类似)

更多推荐

无法填充在SceneBuilder 2中创建的JavaFX TableView

本文发布于:2023-10-06 18:50:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SceneBuilder   JavaFX   TableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!