I didn't find the question where I got an answer for fixing the columns in a tableView/treeTableView.
I would like to do the same for the rows in the table, but applying the same idea I could't manage to achieve anything.
For the columns the idea was to fix the cells' X position to scrolling.
So I tried the same thing with the rows, but instead fixing their Y position to vertical scrolling, but it seems nothing happened.
Here is the code you can check:
Controller.java
:
public class Controller implements Initializable {
@FXML
private TreeTableView<Model> table;
@FXML
private TreeTableColumn<Model, String> col;
@Override
public void initialize(URL location, ResourceBundle resources) {
col.setCellValueFactory(data -> data.getValue().getValue().text);
setup();
table.skinProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
TreeTableRow<Model> firstRow = (TreeTableRow<Model>) table
.queryAccessibleAttribute(AccessibleAttribute.ROW_AT_INDEX, 0);
ScrollBar scrollBar = (ScrollBar) table
.queryAccessibleAttribute(AccessibleAttribute.VERTICAL_SCROLLBAR);
scrollBar.valueProperty().addListener((obs, oV, nV) -> {
firstRow.toFront();
System.out.println("norm: " + nV); // debug
double value = convert(nV.doubleValue(), 0, table.getHeight()) / 2;
System.out.println("non-norm: " + value); // debug
firstRow.setTranslateY(value);
});
}
});
}
private void setup() {
table.setShowRoot(false);
table.setRoot(new TreeItem<>());
Model r1 = new Model("A");
Model r2 = new Model("B");
Model r11 = new Model("A1");
Model l1 = new Model("Ba");
Model l2 = new Model("Bb");
TreeItem<Model> ti = new TreeItem<>(r2);
ti.getChildren().add(new TreeItem<>(l1));
ti.getChildren().add(new TreeItem<>(l2));
table.getRoot().getChildren().add(new TreeItem<>(r11));
table.getRoot().getChildren().add(ti);
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
table.getRoot().getChildren().add(new TreeItem<>(r1));
}
private double convert(double val, double min, double max) {
return val * (max - min) + min;
}
public static class Model {
private StringProperty text;
public Model(String text) {
this.text = new SimpleStringProperty(text);
}
public StringProperty textProperty() {
return text;
}
}
}
View.fxml
:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TreeTableColumn?>
<?import javafx.scene.control.TreeTableView?>
<?import javafx.scene.layout.StackPane?>
<StackPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="stackoverflow.fixrow.Controller">
<TreeTableView fx:id="table">
<columns>
<TreeTableColumn fx:id="col" prefWidth="200"/>
</columns>
</TreeTableView>
</StackPane>
After debugging a while I figured out that the vertical scroll's values differ from the horizontal ones. The vertical scroll has normalized values, so I tought ohh that was the problem I was trying to work with normalized values that's why its not working, but after converting it back to real values It seems the same happens, so nothing.
Any idea what did I miss, or why isn't working the same idea like at the columns?
Of course I would appreciate any other working solution.
Note: Here I have a TreeTableView
, because at the moment I need it for that, but I think the same solution can be applied for both TV
and TTV