我已阅读有关编辑TableView的所有主题,但我无法找到适合我案例的解决方案。我提出这个问题,因为我有一个小应用程序,处理ScoreBoardCard,这是mysql数据库表。因此,当我进入我应该编辑TableView的阶段时,我按下我想要编辑的列没有任何事情发生,更新db的代码什么也没做,因为我没有更改单元格。这是代码我的舞台:
public class UnosCiljanihVrednosti {
static Stage stgAdminAdding = new Stage();
static private ObservableList<Scb>data1;
ViewAdmin object=new ViewAdmin();
public static TableView<Scb> table1;
static String c1;
static TextField perspektiva;
static TextField cilj;
static TextField mera;
static TextField ciljanavrednost;
static ComboBox comboBox;
static ResultSet rs = null;
public static void unosCiljanihVrednosti() {
BorderPane root = new BorderPane();
HBox hbox = new HBox();
Label naslov = new Label("PERFORMANSE-NOVE CILJANE VREDNOSTI");
naslov.setFont(new Font(24));
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().add(naslov);
hbox.setAlignment(Pos.CENTER);
Insets ins1 = new Insets(5, 10, 10, 10);
hbox.setPadding(ins1);
javafx.scene.layout.VBox vboxMenuAdmin = new javafx.scene.layout.VBox();
vboxMenuAdmin.setAlignment(Pos.CENTER);
Insets ins = new Insets(5, 10, 10, 10);
vboxMenuAdmin.setPadding(ins);
Button btSacuvaj = new Button("Sačuvaj");
btSacuvaj.setMinSize(100, 50);
btSacuvaj.setOnAction((ActionEvent event) -> {
sacuvaj();
});
HBox hboxTabela=new HBox();
Insets ins3 = new Insets(50,850, 50, 50);
hboxTabela.setPadding(ins3);
table1 = new TableView<>();
data1= FXCollections.observableArrayList();
loadDataFromDataBase(table1);
table1.setEditable(true);
table1.getSelectionModel().setCellSelectionEnabled(true);
TableColumn<Scb, String> column1=new TableColumn("Perspektiva");
column1.setMinWidth(350);
column1.setCellValueFactory(new PropertyValueFactory<>("perspektiva"));
TableColumn<Scb, String> column2=new TableColumn("Cilj");
column2.setMinWidth(400);
column2.setCellValueFactory(new PropertyValueFactory("cilj"));
TableColumn<Scb, String> column3=new TableColumn("Mera");
column3.setMinWidth(350);
column3.setCellValueFactory(new PropertyValueFactory("mera"));
TableColumn<Scb, Double> column4=new TableColumn("Ciljana Vrednost");
column4.setMinWidth(400);
column4.setCellValueFactory(new PropertyValueFactory("ciljanavrednost"));
table1.getColumns().addAll(column1,column2,column3,column4);
column4.setOnEditCommit(event -> {
Scb scb = event.getRowValue();
scb.setCiljanavrednost(event.getNewValue());
updateData("ciljanavrednost", event.getNewValue(), scb.getMera());
});
HBox hbox4=new HBox();
Insets ins4 = new Insets(50,350, 50, 50);
hbox4.setPadding(ins4);
HBox hboxDugmici=new HBox();
Insets ins2 = new Insets(50, 50, 50, 1450);
hboxDugmici.setPadding(ins2);
hboxDugmici.setSpacing(20);
hboxDugmici.getChildren().addAll(btSacuvaj);
HBox donjiHBox=new HBox();
donjiHBox.getChildren().addAll(hboxDugmici);
//Image logo=new Image("logo.png");
//ImageView vLogo=new ImageView(logo);
Button otvaranje = new Button("Dugme");
// vboxMenuAdmin.getChildren().addAll(btDodavanje, btBrisanje);
// menu.getChildren().add(menuBar);
root.setTop(hbox);
root.setCenter(table1);
root.setBottom(donjiHBox);
root.setRight(hbox4);
Scene scene = new Scene(root, 1920, 1000);
/* ViewAdmin.stgAdmin.setTitle("");
ViewAdmin.stgAdmin.setResizable(false);
ViewAdmin.stgAdmin.setScene(scene);
ViewAdmin.stgAdmin.show();
*/
stgAdminDodavanje.setTitle("");
stgAdminDodavanje.setResizable(false);
stgAdminDodavanje.setScene(scene);
stgAdminDodavanje.show();
}
private static void sacuvaj() {
stgAdminDodavanje.close();
ViewAdmin.stgAdmin.show();
}
public static void loadDataFromDataBase(TableView table){
try {
Class.forName("com.mysql.jdbc.Driver");
CONNECTION =DriverManager.getConnection(URL, USERNAME, PASSWORD);
data1=FXCollections.observableArrayList();
ResultSet rs= CONNECTION.createStatement().executeQuery("SELECT* FROM bsc");
while(rs.next()){
data1.add(new Scb(rs.getString("perspektiva"),rs.getString("cilj"),rs.getString("mera"),rs.getDouble("ciljanavrednost")));
System.out.println(rs.getString("cilj"));
}
CONNECTION.close();
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(AzuriranjePostojecihPerformansi.class.getName()).log(Level.SEVERE, null, ex);
}
table.setItems(data1);
}
public void getRow() {
TablePosition pos = table1.getSelectionModel().getSelectedCells().get(0);
int row = pos.getRow();
TableColumn col = pos.getTableColumn();
String data1 = (String) col.getCellObservableValue(row).getValue();
System.out.println(data1);
}
private static void updateData(String column, Double newValue, String id) {
try (
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/cs102-projekat", "root", "");
PreparedStatement stmt = connection.prepareStatement("UPDATE bsc SET ciljanavrednost = ? WHERE mera = ?");
) {
stmt.setDouble(1, newValue);
stmt.setString(2, id);
stmt.execute();
} catch (SQLException ex) {
System.err.println("Error");
// if anything goes wrong, you will need the stack trace:
ex.printStackTrace(System.err);
}
}
}
答案 0 :(得分:0)
试试这个:
column4.setCellFactory(TextFieldTableCell.forTableColumn());
column4.setOnEditCommit(
new EventHandler<CellEditEvent<Scb, Double>>() {
@Override
public void handle(CellEditEvent<Scb, Double> t) {
((Scb) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setYourField(t.getNewValue());
//updateYourData((Scb) t.getRowValue());
} });