如何在JavaFX 9中的TableView中查找可见行的索引

如何在JavaFX 9中的TableView中查找可见行的索引,第1张

如何在JavaFX 9中的TableView中查找可见行的索引

我遇到了与使用JavaFX 8特定的TableViewSkin /
VirtualFlow代码相同的问题,但是现在使用此变通办法来解决此问题的另一篇文章中的一些建议-
请参阅Tableview可见行

此替代方法在Java8 /
9中运行和编译,并且不使用参考包com.sun.javafx。要使用该类,您需要在构造TableView和向其中添加行之间创建TableViewExtra:

TableView<YourRowType> tableView = new TableView<YourRowType>();TableViewExtra<YourRowType> tvX = new TableViewExtra(tableView);// Add some rows etc

然后,您可以致电:

int firstVisRowIndex = tvX.getFirstVisibleIndex();int lastVisRowIndex  = tvX.getLastVisibleIndex();txV.scrollToSelection();

您可以看到我曾经在注释中使用的旧com.sun.javafx依赖项:

import java.util.linkedHashSet;//com.sun.javafx: START//import com.sun.javafx.scene.control.skin.TableViewSkin;//import com.sun.javafx.scene.control.skin.VirtualFlow;//import javafx.beans.value.ChangeListener;//import javafx.beans.value.Observablevalue;//import javafx.scene.Node;//import javafx.scene.control.IndexedCell;//import javafx.scene.control.Skin;// com.sun.javafx: ENDimport javafx.collections.ObservableList;import javafx.scene.control.TableRow;import javafx.scene.control.TableView;import javafx.util.Callback;public class TableViewExtra<T> // com.sun.javafx: implements ChangeListener<Skin<?>>{    private final TableView<T> tableView;    // com.sun.javafx: private VirtualFlow<?> flow;    linkedHashSet<TableRow<T>> rows = new linkedHashSet<>();    private int firstIndex;    private int lastIndex;    public TableViewExtra(TableView<T> tableView)    {        this.tableView = tableView;        // com.sun.javafx: tableView.skinProperty().addListener(this);        // Callback to monitor row creation and to identify visible screen rows        final Callback<TableView<T>, TableRow<T>> rf = tableView.getRowFactory();        final Callback<TableView<T>, TableRow<T>> modifiedRowFactory = new Callback<TableView<T>, TableRow<T>>() { @Override public TableRow<T> call(TableView<T> param) {     TableRow<T> r = rf != null ? rf.call(param) : new TableRow<T>();     // Save row, this implementation relies on JaxaFX re-using TableRow efficiently     rows.add(r);     return r; }        };        tableView.setRowFactory(modifiedRowFactory);    }    // com.sun.javafx BEGIN//    public void changed(Observablevalue<? extends Skin<?>> ov, Skin<?> t, Skin<?> t1)//    {//        if (t1 == null) {// return;//        }////        TableViewSkin<?> tvs = (TableViewSkin<?>) t1;//        ObservableList<Node> kids = tvs.getChildren();////        if (kids == null || kids.isEmpty() || kids.size() < 2) {// return;//        }//        flow = (VirtualFlow<?>) kids.get(1);//    }    // com.sun.javafx END        public void    scrollToIndex(int ... indices)    {        int first = getFirstVisibleIndex();        int last = getLastVisibleIndex();        int where = first;        boolean changeScrollPos = true;        // No point moving current scroll position if one of the index items is visible already:        if (first >= 0 && last >= first) for (int idx : indices) {     if (first <= idx && idx <= last)     {         changeScrollPos = false;         break;     } }        if (indices.length > 0 && changeScrollPos)        { where = indices[0]; if (first >= 0) {     int x = closestTo(indices, first);     int abs = Math.abs(x - first);     if (abs < Math.abs(where - first))     {         where = x;     } } if (last >= 0) {     int x = closestTo(indices, last);     int abs = Math.abs(x - last);     if (abs < Math.abs(where - last))     {         where = x;     } } tableView.scrollTo(where);        }    }    private static int closestTo(int[] indices, int value)    {        int x    = indices[0];        int diff = Math.abs(value - x);        int newDiff = diff;        for (int v : indices)        { newDiff = Math.abs(value - v); if (newDiff < diff) {     x    = v;     diff = newDiff; }        }        return x;    }    private void recomputeVisibleIndexes()    {        firstIndex = -1;        lastIndex = -1;        // Work out which of the rows are visible        double tblViewHeight = tableView.getHeight();        double headerHeight = tableView.lookup(".column-header-background").getBoundsInLocal().getHeight();        double viewPortHeight = tblViewHeight - headerHeight;        for(TableRow<T> r : rows)        { if (!r.isVisible()) continue; // tingyik90 double minY = r.getBoundsInParent().getMinY(); double maxY = r.getBoundsInParent().getMaxY(); boolean hidden  = (maxY < 0) || (minY > viewPortHeight); // boolean fullyVisible = !hidden && (maxY <= viewPortHeight) && (minY >= 0); if (!hidden) {     if (firstIndex < 0 || r.getIndex() < firstIndex)     {         firstIndex = r.getIndex();     }     if (lastIndex < 0 || r.getIndex() > lastIndex)     {         lastIndex = r.getIndex();     } }        }    }        public int getFirstVisibleIndex()    {        // com.sun.javafx: START//        int jre8Index = -1;//        if (flow != null)//        {// IndexedCell<?> cell = flow.getFirstVisibleCell();// if (cell != null)// {//     jre8Index = cell.getIndex();// }//        }        // com.sun.javafx: END        recomputeVisibleIndexes();        System.out.println("getFirstVisibleIndex "+firstIndex+" rows="+rows.size());        return firstIndex;    }        public int getLastVisibleIndex()    {        // com.sun.javafx: END//        int jre8Index = -1;//        if (flow != null)//        {// IndexedCell<?> cell = flow.getLastVisibleCell();// if (cell != null)//     jre8Index = cell.getIndex();//        }        // com.sun.javafx: END        recomputeVisibleIndexes();        System.out.println("getLastVisibleIndex "+lastIndex+" rows="+rows.size());        return lastIndex;    }        public void scrollToSelection()    {        ObservableList<Integer> seln = tableView.getSelectionModel().getSelectedIndices();        int[] indices = new int[seln.size()];        for (int i = 0; i < indices.length; i++)        { indices[i] = seln.get(i).intValue();        }        scrollToIndex(indices);    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5430023.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存