【问题标题】:JavaFX: Extracting data from a TableViewJavaFX:从 TableView 中提取数据
【发布时间】:2015-09-17 20:13:24
【问题描述】:

我目前正在使用一个应用程序,该应用程序允许用户在给定文件中搜索他们可能要查找的数据,并且一切正常。

下一个实现是允许用户填充自己的数据并将其保存到单独的文件中。

由于搜索数据的视图区域与我制作用于写入新数据的表单的方式相同,因此我认为我会重复使用这些字段。

但是,我有一个 TableView 作为其中一项,我不确定您如何从表格中提取信息。

我假设由于 TableView 填充了一个自定义 Row 类,因此我可以检索组成表格的一组 Rows 并以这种方式解构它们,但我无法确定如何最好地完成。

同时,我正在尝试学习如何覆盖 TableView 的单元格工厂的元素以使单元格可编辑(因为可编辑默认情况下不执行任何操作?),因此这可能是一个整合策略的地方。

【问题讨论】:

    标签: javafx tableview


    【解决方案1】:

    您只需要在表上调用getItems() 并遍历返回的列表。假设一切都以正常的“JavaFX 方式”设置,列表元素中的任何属性都将由编辑机制自动更新。

    这是一个完整的、基于 FXML 的示例。

    EditableTableExample.fxml(在包application中):

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.layout.BorderPane?>
    <?import javafx.scene.control.TableView?>
    <?import javafx.scene.control.TableColumn?>
    <?import javafx.scene.control.cell.TextFieldTableCell?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.geometry.Insets?>
    <?import javafx.scene.control.Button?>
    
    <BorderPane xmlns:fx="http://javafx.com/fxml/1"
        fx:controller="application.EditableTableViewController">
        <center>
            <TableView fx:id="table" editable="true">
                <columns>
                    <TableColumn text="First Name" prefWidth="150"
                        fx:id="firstNameColumn">
                        <cellFactory>
                            <TextFieldTableCell fx:factory="forTableColumn" />
                        </cellFactory>
                    </TableColumn>
                    <TableColumn text="Last Name" prefWidth="150" fx:id="lastNameColumn">
                        <cellFactory>
                            <TextFieldTableCell fx:factory="forTableColumn" />
                        </cellFactory>
                    </TableColumn>
                    <TableColumn text="Email" prefWidth="150" fx:id="emailColumn">
                        <cellFactory>
                            <TextFieldTableCell fx:factory="forTableColumn" />
                        </cellFactory>
                    </TableColumn>
                </columns>
            </TableView>
        </center>
        <bottom>
            <HBox alignment="CENTER">
                <padding>
                    <Insets top="10" right="10" left="10" bottom="10" />
                </padding>
                <children>
                    <Button onAction="#showData" text="Show Data" />
                </children>
            </HBox>
        </bottom>
    </BorderPane>
    

    可编辑表格控制器:

    package application;
    
    import javafx.fxml.FXML;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    
    
    public class EditableTableViewController {
    
        @FXML
        private TableView<Person> table ;
    
        @FXML
        private TableColumn<Person, String> firstNameColumn ;
    
        @FXML
        private TableColumn<Person, String> lastNameColumn ;
    
        @FXML
        private TableColumn<Person, String> emailColumn ;
    
        public void initialize() {
            firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
            lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
            emailColumn.setCellValueFactory(cellData -> cellData.getValue().emailProperty());
    
            table.getItems().addAll(
                    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")
            ) ;
        }
    
        @FXML
        private void showData() {
            for (Person person : table.getItems()) {
                String formatted = String.format("%s %s (%s)", person.getFirstName(), person.getLastName(), person.getEmail());
                System.out.println(formatted);
            }
            System.out.println();
        }
    }
    

    模型类Person:

    package application;
    
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
    
    public class Person {
        private final StringProperty firstName = new SimpleStringProperty(this, "firstName");
        public final String getFirstName() {
            return firstNameProperty().get();
        }
        public final void setFirstName(String firstName) {
            firstNameProperty().set(firstName);
        }
        public StringProperty firstNameProperty() {
            return firstName ;
        }
    
        private final StringProperty lastName = new SimpleStringProperty(this, "lastName");
        public final String getLastName() {
            return lastNameProperty().get();
        }
        public final void setLastName(String lastName) {
            lastNameProperty().set(lastName);
        }
        public StringProperty lastNameProperty() {
            return lastName ;
        }
    
        private final StringProperty email = new SimpleStringProperty(this, "email");
        public final String getEmail() {
            return emailProperty().get();
        }
        public final void setEmail(String email) {
            emailProperty().set(email);
        }
        public StringProperty emailProperty() {
            return email ;
        }
    
        public Person(String firstName, String lastName, String email) {
            this.setFirstName(firstName);
            this.setLastName(lastName);
            this.setEmail(email);
        }
    }
    

    应用类:

    package application;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.layout.BorderPane;
    import javafx.stage.Stage;
    
    
    public class EditableTableViewExample extends Application {
        @Override
        public void start(Stage primaryStage) throws Exception {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("EditableTableExample.fxml"));
            BorderPane root = loader.load();
            Scene scene = new Scene(root, 800, 600);
            primaryStage.setScene(scene);       
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    该按钮仅用于演示,但按下它会调用控制器中的showData() 方法,该方法会遍历表的项目列表并从每个项目中检索属性值。如果双击要编辑的单元格,键入以更改值,然后按 Enter 提交编辑,然后按该按钮将显示更新的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      • 2016-04-21
      • 2017-12-13
      • 1970-01-01
      • 2016-04-07
      • 2013-06-27
      • 2012-11-03
      相关资源
      最近更新 更多