2015-10-05 64 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