2015-10-05 32 views
0

當我的數據庫信息導入TableView時,所有內容都顯示正確但不可編輯,我無法弄清楚是什麼導致了這種情況。任何人都可以發現問題嗎?如何更新TableView中的ObservableList以寫入數據庫

@FXML 
public TableView<Clients> activeclients; 

@FXML 
public void databaseConnection(ActionEvent event) { 
    Connection c = null; 

    ObservableList data = FXCollections.observableArrayList(); 

    try { 
     c = DriverManager.getConnection("jdbc:sqlite:db/clients.db"); 
     c.setAutoCommit(false); 
     System.out.println("Opened Database Successfully"); 
     String SQL = "SELECT * from clientstest"; 

     //ResultSet 
     ResultSet rs = c.createStatement().executeQuery(SQL); 

     /** 
     * ******************************** 
     * TABLE COLUMN ADDED DYNAMICALLY * ******************************** 
     */ 
     for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) { 
      //We are using non property style for making dynamic table 
      final int j = i; 

      TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i + 1)); 
      col.setCellFactory(TextFieldTableCell.<Clients>forTableColumn()); 

col.setOnEditCommit(e -> { 
       ObservableList row = e.getRowValue(); 
       row.set(j, e.getNewValue()); 
      }); 

      col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() { 

       @Override 
       public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) { 
        return new SimpleStringProperty(param.getValue().get(j).toString()); 
       } 
      }); 
      col.setMinWidth(175); 

      activeclients.getColumns().addAll(col); 
      System.out.println("Column [" + i + "] "); 
      col.setEditable(true); 
      activeclients.setEditable(true); 
     } 

     /** 
     * ****************************** 
     * Data added to ObservableList * ****************************** 
     */ 
     while (rs.next()) { 
      //Iterate Row 
      ObservableList<String> row = FXCollections.observableArrayList(); 
      for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { 
       //Iterate Column 
       row.add(rs.getString(i)); 
      } 
      System.out.println("Row [1] added " + row); 
      data.add(row); 

     } 

     //FINALLY ADDED TO TABLEVIEW 
     activeclients.setItems(data); 
     activeclients.setEditable(true); 

    } catch (Exception e) { 
     System.err.println(e.getClass().getName() + ": " + e.getMessage()); 
     System.exit(0); 
    } 

} 

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

}

CLIENTS CLASS

包mach1jedi.model;

import javafx.beans.property.SimpleStringProperty;

公共類客戶{

private SimpleStringProperty fname; 
private SimpleStringProperty lname; 
private SimpleStringProperty clientID; 
private SimpleStringProperty provcontractnum; 
private SimpleStringProperty adscode; 
private SimpleStringProperty transcode; 
private SimpleStringProperty ccucontractnum; 

public SimpleStringProperty getFname() { 
    return fname; 
} 

public SimpleStringProperty getLname() { 
    return lname; 
} 

public SimpleStringProperty getClientID() { 
    return clientID; 
} 

public SimpleStringProperty getProvcontractnum() { 
    return provcontractnum; 
} 

public SimpleStringProperty getAdscode() { 
    return adscode; 
} 

public SimpleStringProperty getTranscode() { 
    return transcode; 
} 

public SimpleStringProperty getCcucontractnum() { 
    return ccucontractnum; 
} 

public void setFname(SimpleStringProperty fname) { 
    this.fname = fname; 
} 

public void setLname(SimpleStringProperty lname) { 
    this.lname = lname; 
} 

public void setClientID(SimpleStringProperty clientID) { 
    this.clientID = clientID; 
} 

public void setProvcontractnum(SimpleStringProperty provcontractnum) { 
    this.provcontractnum = provcontractnum; 
} 

public void setAdscode(SimpleStringProperty adscode) { 
    this.adscode = adscode; 
} 

public void setTranscode(SimpleStringProperty transcode) { 
    this.transcode = transcode; 
} 

public void setCcucontractnum(SimpleStringProperty ccucontractnum) { 
    this.ccucontractnum = ccucontractnum; 
} 

}

+0

您的視圖是否包含多個表? – DeathWish

+0

是的,表是在tabpane中,第二個選項卡也有一個表,現在發佈FxML,我把第二個表拿出來,仍然是同樣的問題 – monopolyman

+0

你在哪裏設置列的單元格工廠? –

回答

1

您沒有設置表中的列一個cellFactory。默認的單元工廠提供了一個不支持編輯的單元(基本上單元只是一個標籤)。您需要

col.setCellFactory(TextFieldTableCell.forTableColumn()); 

這將爲列提供基於TextField的編輯單元格。

爲了讓ObservableList<ObservableList>更新,因爲你的表不使用標準的JavaFX的特性模式,您還需要註冊一個處理器編輯承諾:

col.setOnEditCommit(e -> { 
    ObservableList row = e.getRowValue(); 
    row.set(j, e.getNewValue()); 
}); 

請注意,您使用的是全一堆原始類型在你的代碼中,你應該從編譯器中得到一堆警告。您可能需要修正這些以使用正確的類型以獲得上面的代碼進行編譯。 (例如,您應該有TableView<ObservableList<String>>,TableColumn<ObservableList<String>, String>等)

+0

我更新了代碼並學習了一些教程,但仍然被拋棄通過在setOnEditComit代碼中添加它之後:它說它找不到符號getRowValue和getNewValue – monopolyman

相關問題