Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This method creates a treetableview dynamically. The problem is when I scroll to see the whole table or expand/collapse a treeitem, the position of word "yes" which is at first on cells which have same row and column name, changes.

Java
<pre lang="java">

	private TreeTableView<String> drawTable() {

		root.setExpanded(true);

		// Set<String> combinedKeys = new HashSet<>(dc.getCombiFunc().keySet());
		Set<String> funcAllKeys = new HashSet<>(dc.getSortedfuncAll().keySet());
		funcAllKeys.removeAll(dc.getCombiFunc().keySet());
		for (List<String> value : dc.getCombiFunc().values()) {
			funcAllKeys.removeAll(value);
		}
		for (String valueremained : funcAllKeys) {
			ArrayList<String> tempNameId = new ArrayList<>();
			tempNameId.add(dc.getSortedfuncAll().get(valueremained));
		
			root.getChildren().add(new TreeItem<String>(tempNameId.get(0)));
		}

		// ////////////////treetable////////////////////////////
		/**
		 * makes treeTable and set the nodes to it.
		 * */

		treeTable.setRoot(root);
		Arrays.stream(dc.getRootKeys()).forEach(
				rootKey -> root.getChildren().add(
						createTreeItem(dc.getCombiFunc(), rootKey)));

		// ////////////////First column/////////////////////////
		/**
		 * creates first column with no caption and sets treeview for that.
		 * */
		TreeTableColumn<String, String> firstColumn = new TreeTableColumn<>("");
		treeTable.getColumns().add(firstColumn);// Tree column
		firstColumn.setPrefWidth(80);
		firstColumn.setEditable(false);
		firstColumn.setSortable(false);
		firstColumn
				.setCellValueFactory(new Callback<CellDataFeatures<String, String>, ObservableValue<String>>() {
					public ObservableValue<String> call(
							CellDataFeatures<String, String> p) {
						return new ReadOnlyStringWrapper(p.getValue()
								.getValue());
					}
				});

		// //////////////////Rest Columns////////////////////////
		/**
		 * go through all organizations which have a function and make column
		 */

		for (Entry<String, String> ent : dc.getSortedAssignedOrg().entrySet()) {
			TreeTableColumn<String, ArrayList<String>> col = new TreeTableColumn<>();

			Label label = new Label(ent.getValue());
			col.setGraphic(label);
			col.setEditable(false);
			col.setSortable(false);
			label.setTooltip(new Tooltip(label.getText()));// tooltip for column
			col.setPrefWidth(45);
			// cell Value Factory////////////////////////

			TreeMap<String, List<String>> temp = (TreeMap<String, List<String>>) dc
					.getFuncTypeOrg().clone();

//			for (int i = 0; i < dc.getFuncTypeOrg().size(); i++) {

				List<String> list = temp.firstEntry().getValue();

				String key = temp.firstEntry().getKey();

				// //////////////cellfactory/////////////////////////
				col.setCellFactory(new Callback<TreeTableColumn<String, ArrayList<String>>, TreeTableCell<String, ArrayList<String>>>() {
					@Override
					public TreeTableCell<String, ArrayList<String>> call(
							TreeTableColumn<String, ArrayList<String>> param) {
						return new TreeTableCell<String, ArrayList<String>>() {
							private List<Double> result = new ArrayList<Double>();
							private List<Double> weight = new ArrayList<>();
							private double lastresult = new Double(0);

							public void updateItem(ArrayList<String> item,
									boolean empty) {

								super.updateItem(item, empty);

								// System.out.println(this.getTreeTableRow()
								// .getItem());
								// System.out.println(treeTable.getTreeItem(getIndex()).getValue());
								for (List<String> entry : dc.getFuncTypeOrg()
										.values()) {
									
									if (entry.get(1).equals(
											this.getTreeTableRow().getItem())
											&& label.getText().equals(
													entry.get(5))) {
										setText("here");
									}
								}

							}

						};
					};

				});// end cell factory

			treeTable.getColumns().add(col);
		}
		// end for col

		treeTable.setPrefWidth(1200);
		treeTable.setPrefHeight(500);
		treeTable.setShowRoot(false);
		treeTable.setEditable(false);

		treeTable.setTableMenuButtonVisible(true);

		return treeTable;
	}

	/**
	 * Creates a TreeItem for a TreeView from a set of data given the data and
	 * an identified root within the data.
	 * 
	 * @param data
	 * @param rootKey
	 * @return item
	 */
	private TreeItem<String> createTreeItem(TreeMap<String, List<String>> data,
			String rootKey) {
		TreeItem<String> item = new TreeItem<>();
		item.setValue(rootKey);
		item.setExpanded(true);

		List<String> childData = data.get(rootKey);
		if (childData != null) {
			childData.stream().map(child -> createTreeItem(data, child))
					.collect(Collectors.toCollection(item::getChildren));
		}

		String valueName = item.getValue();
		// String sorteV = dc.getSortedfuncAll().get(valueName);
		item.setValue((dc.getSortedfuncAll().get(valueName)));

		return item;
	}

}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900