我有一个带有动态列的javafx tableview,从某种意义上说,这些列是在运行时添加的。我没有使用任何模型类,并且通过可观察的列表填充了数据。在表的初始加载之后,数据就可以了,当我滚动浏览表,表的初始值被加载回去时出现问题,或者如果表为空,则输入的所有数据都将被清除。这是我的代码
这是我的联系
public Connection conn;
TableColumn tc = null;
TableView <ObservableList> table = new TableView<>();
ObservableList items;
public Connection dbcon() {
try {
String url ="jdbc:mysql://localhost:3306/"; //koma //emerald
conn = DriverManager.getConnection(url, "root", "walgotech");
String createdb = "CREATE DATABASE IF NOT EXISTS `school`";
PreparedStatement pst = conn.prepareStatement(createdb);
pst.execute();
Statement stt = conn.createStatement();
stt.execute("use school");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
这是我最初加载数据的开始方法
public void start(Stage primaryStage) {
StackPane root = new StackPane();
table.setEditable(true);
try{
items = FXCollections.observableArrayList();
items.clear();
dbcon();
List<String> sql1 = new ArrayList<String>();
ResultSet mk111 = conn.prepareStatement("SELECT * FROM `subjects1` ORDER BY `ORDER` ASC").executeQuery();;
while(mk111.next()){
sql1.add("`"+mk111.getString("code")+"`");
}
String formattedString1 = sql1.toString()
.replace("[", "")
.replace("]", "")
.trim();
ResultSet rs =conn.prepareStatement("SELECT `form4 term1 mid-term exam 2019`."
+ "admno,CONCAT(`FIRST NAME`,' ',`SECOND NAME`)"
+ " AS Studentname,"+formattedString1+""
+ ",`form4 term1 mid-term exam 2019`.Total FROM `form4 term1 mid-term exam 2019` "
+ "INNER JOIN registration on `form4 term1 mid-term exam 2019`."
+ "admno=registration.`ADM NO` GROUP BY `form4 term1 mid-term exam 2019`.admno ").executeQuery();
for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++){
final int j = i;
tc = new TableColumn <>(rs.getMetaData().getColumnName(i+1));
tc.setEditable(true);
tc.setCellFactory(column -> new EditCell1(null));
tc.setPrefWidth(90);
tc.setCellValueFactory(new Callback<CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
table.getColumns().addAll(tc);
}
while(rs.next()){
ObservableList<String> row = FXCollections.observableArrayList();
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
row.add(rs.getString(i));
}
items.add(row);
}
table.setItems(items);
table.setOnKeyPressed(event -> {
TablePosition<ObservableList, ?> pos = table.getFocusModel().getFocusedCell() ;
if (pos != null) {
table.edit(pos.getRow(), pos.getTableColumn());
}
});
root.getChildren().add(table);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
} catch (Exception e1) {
e1.printStackTrace();
}
}
以下类和方法用于编辑单元格。开始编辑,提交编辑并取消。
private class EditCell1 extends TableCell<ObservableList, String> {
private final TextField textField = new TextField();
private final Function<ObservableList, StringProperty> property ;
@SuppressWarnings("unchecked")
EditCell1(Function<ObservableList, StringProperty> property) {
this.property = property ;
textProperty().bind(itemProperty());
setGraphic(textField);
setContentDisplay(ContentDisplay.TEXT_ONLY);
textField.requestFocus();
textField.setOnAction(evt -> {
commitEdit(textField.getText());
});
textField.setOnKeyPressed(new EventHandler(){
@Override
public void handle(Event arg0) {
}
});
}
@Override
public void startEdit() {
super.startEdit();
textField.setText(getItem());
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
textField.requestFocus();
}
@Override
public void cancelEdit() {
super.cancelEdit();
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
@Override
public void commitEdit(String text) {
super.commitEdit(text);
{
final ObservableList<TablePosition> selectedCells = table.getSelectionModel().getSelectedCells();
for (TablePosition pos : selectedCells) {
String examvalue=textField.getText();
System.out.println(examvalue+" where admno="+table.getColumns().get(0).getCellObservableValue(pos.getRow()).getValue());
table.getSelectionModel().selectBelowCell();
}
}
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
}