Search code examples
javafxjavafx-8

Select first item shown into a treeTableView in JavaFX 8


I have a TreeTableView control with several items. The user can move with the side scroll bar to any part of the table. How can I know which is the first item that the table shows? I mean, the first item we see. I have been going through the methods offered by this control and I have seen that scrollTo shows an item according to an order number that we specify but I have not found anything that is something like getFirstItemShown. I imagine that for a TableView control it should work the same, right? I'm using JavaFX 8.


Solution

  • Just for fun:

    (note that if there are cut lines it can choose the next row, and the first row is sometimes hidden under the Column)

    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.stage.Stage;
    import javafx.beans.property.ReadOnlyStringWrapper;
    import javafx.scene.control.TreeTableColumn;
    import javafx.scene.control.TreeTableColumn.CellDataFeatures;
    import javafx.scene.control.TreeItem;
    import javafx.scene.control.TreeTableView;
    
    
    public class Main extends Application {
    
        @Override
        public void start(Stage primaryStage) throws Exception{
    
            primaryStage.setTitle("Tree View Sample");
            TreeItem<String> rootItem = new TreeItem<String> ("Inbox");
            rootItem.setExpanded(true);
    
            for (int i = 1; i < 888; i++) {
                TreeItem<String> item = new TreeItem<String> ("Message" + i);
                rootItem.getChildren().add(item);
            }
            TreeTableColumn<String, String> column = new TreeTableColumn<>("Column");
            column.setPrefWidth(150);
            column.setCellValueFactory((CellDataFeatures<String, String> p) -> new ReadOnlyStringWrapper(
                    p.getValue().getValue()));
            TreeTableView<String> treeTableView = new TreeTableView<>(rootItem);
            treeTableView.getColumns().add(column);
            primaryStage.setScene(new Scene(treeTableView, 300, 250));
            primaryStage.show();
    
            Platform.runLater(()->{
                ScrollBar verticalBar = (ScrollBar) treeTableView.lookup(".scroll-bar");
                verticalBar.valueProperty().addListener(new ChangeListener<Number>() {
                    @Override
                    public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                        int currentRow = 0;
                        if(newValue.doubleValue()<1){
                            currentRow =(int)((newValue.doubleValue()*treeTableView.getExpandedItemCount())-(newValue.doubleValue()*verticalBar.getVisibleAmount())*treeTableView.getExpandedItemCount());
                        }else {
                            currentRow =(int)(treeTableView.getExpandedItemCount()-verticalBar.getVisibleAmount()*treeTableView.getExpandedItemCount());
                        }
                        System.out.println(currentRow);
                    }
                });
            });
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }