繁体   English   中英

javafx无法编辑TableView行数据

[英]javafx unable to edit tableview row data

我想编辑tableview行数据,我有一些代码可以编辑单元格数据,但是在我的项目fxml代码中不起作用:

<TableView fx:id="table" layoutX="112.0" layoutY="25.0" prefHeight="430.0" prefWidth="505.0">
                           <columns>
                              <TableColumn fx:id="tablecol" prefWidth="490.0" text="Schedule Item">
                                  <cellValueFactory>
                      <PropertyValueFactory property="firstName" />
                  </cellValueFactory></TableColumn>
                           </columns>
                        </TableView>

在FXML fx:id="tablecol"fx:id="table"被用于

码:

@FXML
    private TableView<person1> table;
 @FXML
    private TableColumn<person1, String> tablecol;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {

 tablecol.setCellFactory(TextFieldTableCell.forTableColumn());
tablecol.setOnEditCommit(
    new EventHandler<CellEditEvent<person1, String>>() {
        @Override
        public void handle(CellEditEvent<person1, String> t) {
            ((person1) t.getTableView().getItems().get(
                t.getTablePosition().getRow())
                ).setFirstName(t.getNewValue());
        }
    }
);         try{
       Class.forName("com.mysql.jdbc.Driver");
            Connection con=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/schoolmanagement","root","root");
            String sql="SELECT * FROM `scheduleitem` ";
           Statement stm=(Statement) con.createStatement();
ResultSet rs=stm.executeQuery(sql);
while(rs.next()){

   table.getItems().add(new person1(rs.getString(2)));
}

        } 
        catch (ClassNotFoundException | SQLException e) {

        }
    }    
 }


    public class person1 {
    private final StringProperty firstName = new SimpleStringProperty("");

    public person1(String firstName) {
        setFirstName(firstName);

    }

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String name) {
        this.firstName.set(name);
    }

    public StringProperty firstNameProperty() {
        return firstName;
    }

请帮助为什么我无法编辑单元格数据。

谢谢。

尽管TableColumn缺省是可编辑的,但TableView不可编辑。 所以你需要

<TableView fx:id="table" editable="true" ...>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM