2015-06-30 70 views
0

我目前使用的應用程序允許用戶通過給定的文件搜索他們可能要查找的數據,並且一切都可以順利進行。JavaFX:從TableView中提取數據

下一個實現是允許用戶填充他們自己的數據並將其保存到單獨的文件中。

由於搜索數據的視圖區域與我將如何製作新數據的表單相同,因此我認爲我只是重新使用這些字段。

但是,我有一個TableView作爲其中一個項目,我不確定如何從表中提取信息。

我假設由於TableView中填充了一個自定義的Row類,我可以檢索到構成表的行集,並以這種方式解構它們,但我無法確定如何最好地完成這些。我試圖學習如何重寫TableView的單元工廠的元素以使單元格可編輯(因爲可編輯默認情況下不會執行任何操作?),所以這可能是鞏固策略的一個地方。

回答

0

你只需要在表上調用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> 

EditableTableController:

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", "[email protected]"), 
       new Person("Isabella", "Johnson", "[email protected]"), 
       new Person("Ethan", "Williams", "[email protected]"), 
       new Person("Emma", "Jones", "[email protected]"), 
       new Person("Michael", "Brown", "[email protected]") 
     ) ; 
    } 

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

Model類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()方法,該方法遍歷表的項目列表並從每個項目中檢索屬性值。如果雙擊一個要編輯的單元格,鍵入以更改該值,然後使用提交編輯輸入,然後按按鈕將顯示更新的值。