[英]TableView editing DB Table in javafx
我已經閱讀了有關編輯TableView的所有主題,但找不到適合我的情況的解決方案。 我提出了這個問題,因為我有一個處理ScoreBoardCard的小型應用程序,它是mysql DB Table。 因此,當我進入要編輯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);
}
}
}
嘗試這個:
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());
} });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.