如何在javafx中将表放在另一个tableview的单元格中?

时间:2014-04-29 08:37:39

标签: java javafx

我想把一个表放在javafx中的另一个表中。现在我可以放表。这里我的模型是hashmap所以基本上我将我的单元格映射到某个键并将hashmap添加到表中。但是调用滚动更新项目然后再次创建所有映射。下面有我的代码片段请告诉我这里做错了什么..

    @Override
    protected void updateItem(final HashMap<String, String> item, boolean empty) {
      super.updateItem(item, empty);

      if (item != null) {
        if (item instanceof HashMap) {
          Set<String> key = item.keySet();
          for (Iterator<String> it = key.iterator(); it.hasNext();) {
            String s = it.next();
            arraylist.add(s);

          }

          final TableView<Map<String, String>> table = new TableView<>();
          table.setPrefHeight(43);
          table.setEditable(true);
          TableColumn datacol[] = new TableColumn[item.size()];

          Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell>() {
            public TableCell call(TableColumn p) {
              EditingCell editingCell = new EditingCell();
              editingCell.setTooltipMessage("Editing  " + p.getText());
              return editingCell;
            }
          };
          for (int i = 0; i < item.size(); i++) {

            datacol[i] = new TableColumn<>(arraylist.get(i));
            datacol[i].setCellValueFactory(new MapValueFactory(arraylist.get(i)));
            datacol[i].setMinWidth(30);
            datacol[i].setCellFactory(cellFactory);
          }

          table.getColumns().addAll(datacol);
          table.setItems(generateDataInMap(((HashMap<String, String>) (item))));

          table.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
              // Get the table header
              Pane header = (Pane) table.lookup("TableHeaderRow");
              if (header != null && header.isVisible()) {
                header.setMaxHeight(0);
                header.setMinHeight(0);
                header.setPrefHeight(0);
                header.setVisible(false);
                header.setManaged(false);
              }
            }
          });
          setGraphic(table);

        }
      }
    }

    private ObservableList<Map<String, String>> generateDataInMap(HashMap<String, String> item) {
      ObservableList<Map<String, String>> allData = FXCollections.observableArrayList();

      allData.add(item);
      return allData;
    }
  }

  @Override
  public TableCell call(TableColumn p) {
    return new InnerTableCell();
  }

0 个答案:

没有答案