我是一个纯粹的新手。我通过迭代添加数据库中的列来创建我的表:
completionHandler
正如您所看到的,我的setOnEditCommit完全没有意义:
public void captureDataSuper() {
Connection c;
ObservableList<ObservableList> data;
data = FXCollections.observableArrayList();
try {
c = KonekDB.createConnection();
//SQL FOR SELECTING ALL OF CUSTOMER
String SQL = "SELECT * from adminsupervisor";
//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));
//now its editable
col.setCellFactory(TextFieldTableCell.<Adminsupervisor>forTableColumn());
//trying to make effect on database after edited with setOnEditCommit
col.setOnEditCommit(
new EventHandler<CellEditEvent<Adminsupervisor, String>>() {
public void handle(CellEditEvent<Adminsupervisor, String> t) {
((Adminsupervisor) t.getTableView().getItems().get(
t.getTablePosition().getRow())).set(j, t.getNewValue());
}
}
);
col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
supervisorTable.getColumns().addAll(col);
System.out.println("Column [" + i + "] ");
}
/**
* ******************************
* 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
supervisorTable.setItems(data);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
这是模型类Adminsupervisor:
col.setOnEditCommit(
new EventHandler<CellEditEvent<Adminsupervisor, String>>() {
public void handle(CellEditEvent<Adminsupervisor, String> t) {
((Adminsupervisor) t.getTableView().getItems().get(
t.getTablePosition().getRow())).set(j, t.getNewValue());
}
}
);
我得到了这个堆栈跟踪:
public class Adminsupervisor {
private String id;
private String username;
private String password;
private String userType;
public String getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getUserType() {
return userType;
}
public void setId(String id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserType(String userType) {
this.userType = userType;
}
void set(int j, String newValue) {
for (j = 0; j < 4; j++) {
if (j == 0) {
setId(newValue);
}
if (j == 2) {
setPassword(newValue);
}
if (j == 3) {
setUserType(newValue);
}
if (j == 1) {
setUsername(newValue);
}
}
try {
Connection c = KonekDB.createConnection();
String SQL = "UPDATE adminsupervisor SET "
+ "username=" + username + ","
+ "password=" + password + ","
+ "userType=" + userType + " WHERE id=" + id + "";
//ResultSet
c.createStatement().executeUpdate(SQL);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error on Building Data");
}
}}
所以编辑后我的单元格不会做任何事情。
答案 0 :(得分:1)
data
被声明为ObservableList<ObservableList> data;
,您将其填入ObservableList
s:
while (rs.next()) {
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
...
data.add(row);
}
然后在onEditCommit
处理程序中执行此操作:
public void handle(CellEditEvent<Adminsupervisor, String> t) {
((Adminsupervisor) t.getTableView().getItems().get(
t.getTablePosition().getRow())).set(j, t.getNewValue());
}
t.getTableView().getItems().get(index)
会返回ObservableList
,您尝试投放到Adminsupervisor
,因为显而易见的原因无效... {/ p>
您需要对项目和处理程序使用相同的类型。您是否想要使用ObservableList
或Adminsupervisor
取决于您......
注意:向TableView
和TableColumn
添加类型参数,编译器应该抱怨这一点。但是,通过使用原始类型,可以防止编译器执行这些检查(尽管如此,您可能会收到有关原始类型的警告)。
此外
for (j = 0; j < 4; j++) {
if (j == 0) {
setId(newValue);
}
if (j == 2) {
setPassword(newValue);
}
if (j == 3) {
setUserType(newValue);
}
if (j == 1) {
setUsername(newValue);
}
}
应该重写为
setId(newValue);
setUsername(newValue);
setPassword(newValue);
setUserType(newValue);
j = 4; // not really neccessary since there is no read access to j
至少可以达到同样的效果。 (但是,您可能无缘无故地在for
周围添加了if
循环,并且应将其删除。)
并且还考虑从j
方法中删除Adminsupervisor.set
作为参数,因为该值从未在方法中使用(在原始版本中,在任何读取之前它被0
覆盖访问发生在for (j = 0; j < 4; j++)
,在改进版本中根本没有读取。
此外,ObservableValue
返回的cellValueFactory
永远不会触发更新。如果您使用ObservableList
作为项类型,则可以使用Bindings
类为特定索引获取ObservableValue
:
col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return Bindings.stringValueAt(param.getValue(), j);
}
});