2017-06-18 86 views
0

我已经阅读了关于编辑TableView的所有线程,但是我找不到解决方案。我提出这个问题,因为我有一个小应用程序,它处理ScoreBoardCard,它是mysql数据库表。所以当我进入舞台时,我应该编辑TableView,当我按下列时,我想不编辑任何事情和更新数据库的代码什么都不做,因为我没有更改单元格。这里是我的舞台的代码:TableView在javafx中编辑数据库表

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

试试这个:方

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()); 
      } }); 
+0

先生,现在我有一些paramters在我的方法来设置了updateData,了updateData(在数据库表,新值的行的名字,我在表视图改变单元格,行的id)。我在参数中放置了新的值的地方是什么?如何调用新值 – Pera

+0

@Pera通过强制转换(Scb)t.getRowValue() –

+0

仍然可以获得新值: – Pera