2021年2月10日星期三

How to refresh tableView?

Hello how to refresh tableView after inserting the record to database the method table.setItems(list); does not update the table and the list.add(student); method does not return the calculated grade so I nedd to retrun the record from database is there a why to update this table in the example below ?

    public class table extends Application {      @Override      public void start(Stage primaryStage) throws Exception {          GridPane grid = new GridPane();          grid.setAlignment(Pos.CENTER);          grid.setHgap(20);          grid.setVgap(20);            TableView<Student> table = new TableView<>();          table.setEditable(true);              StudentDao dao = new StudentDao();              ObservableList<Student> list = dao.loadAll();          table.setItems(list);          TableColumn<Student, String> FirstNameCol = createColumn("FIRST NAME",                  Student::LastNameProperty, 150);          TableColumn<Student, String> LastNameCol = createColumn("LAST NAME",                  Student::FirstNameProperty, 150);          TableColumn<Student, String> GradeCol = createColumn("GRADE",                  Student::GenderProperty, 150);            TableColumn<Student, Void> indexCol = createColumn("N.O", student -> null, 35);            indexCol.setCellFactory(col -> new TableCell<Student, Void>() {              @Override              public void updateIndex(int index) {                  super.updateIndex(index);                  if (isEmpty() || index < 0) {                      setText(null);                  } else {                      setText(Integer.toString(index+1));                  }              }          });            table.getColumns().add(indexCol);          table.getColumns().add(FirstNameCol);          table.getColumns().add(LastNameCol);          table.getColumns().add(GradeCol);          Button btn = new Button("Submit");          grid.add(table,0,0);          grid.add(btn,1,0);          grid.add(txtFirstName,0,2);          grid.add(txtLastName,0,1);          grid.add(txtMath,0,3);          grid.add(txtPhysic,0,3);          grid.add(txtScience,0,3);          btn.setOnAction(event->          {          String firstname = txtFirstName.getText();          String lastname = txtLastName.getText();          String math = txtMath.getText();          String physic = txtPhysic.getText();          String science = txtScience.getText();          Student student = new Student();          student.setFirstName(firstname);          student.setLastName(lastname);          student.setMath(math);          student.setPhysic(physic);          student.setScene(science);          try {              int count = StudentDao.getInstance().insert(student);              if (count == 1) {                   list.clear();                   table.setItems(list);              } else {                  JOptionPane.showMessageDialog(null, "Faild");              }          } catch (Exception ex) {}          });          primaryStage.setScene(new Scene(new BorderPane(grid)));          primaryStage.show();      }  }  
https://stackoverflow.com/questions/66148203/how-to-refresh-tableview February 11, 2021 at 11:06AM

没有评论:

发表评论