在Tableview Javafx中添加记录时如何分隔列。重复记录问题

时间:2019-06-12 18:46:24

标签: java javafx tableview tablecolumn

我添加TableView来通过按下按钮从mysql提取数据并生成列名。问题在于添加记录。因为它复制了它们。

GUI:

             for (Button i : buttonsNameTable) {
                i.setOnAction(event1 -> {
                    int col = database.selectNameColumnt(i.getText()).size();
                      ArrayList<String> list = database.selectNameColumnt(i.getText());

                    result.getChildren().add(dynamicTableView.createColumn(col, list, tableView, database.selectAll(i.getText()) ));

CLASS数据库查询:

    ObservableList<String> selectAll(String nametable){
    ObservableList<String> dataDB = FXCollections.observableArrayList();
     String nameTable  = StringUtils.capitalize(nametable);

  if(nameTable.equals("Filmy")) {
      try {
          tx.begin();
          Query q = pm.newQuery("SELECT FROM " + "model." + nameTable);
          List<Filmy> products = q.executeList();
          Iterator<Filmy> iter = products.iterator();
          while (iter.hasNext()) {
              Filmy p = iter.next();
              dataDB.add(p.getFilmyId() + " ," + p.getTytulFilmu());
          }
          tx.commit();

      } catch (Exception e) {
          System.out.println("nie dodano: " + e.getMessage());
      } finally {
          if (tx.isActive()) {
              tx.rollback();
          }
          pm.close();
      }

      pm = pmf.getPersistenceManager();
      tx = pm.currentTransaction();
  }
    return dataDB;
}

CLASS DynamicTableView生成列和行返回表视图;

        public TableView createColumn(int cols, ArrayList<String> nametable, TableView tableview, ObservableList<String> dataDB) {
    tableview.getColumns().clear();
    try {
        for (int i = 0; i < cols; i++) {
            final int j = i;
            TableColumn col = new TableColumn(nametable.get(i));
            col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<String, String>, ObservableValue<String>>() {
                public ObservableValue<String> call(TableColumn.CellDataFeatures<String, String> param) {
                    return new SimpleStringProperty(param.getValue().toString());
                }
            });
            tableview.getColumns().addAll(col);
        }

        tableview.setItems(dataDB);

    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error on Building Data");
    }
    return tableview;
}

结果:

https://i.imgur.com/Lvahe8N.jpg

0 个答案:

没有答案