Desktop Java
JavaFX Tip 15: ListView Autoscrolling
I recently had to implement autoscrolling functionality for FlexGanttFX and thought that my solution might be useful for others. You find the basic concepts of it in the listing below. The main idea is that a background thread is used to adjust the pixel location of the virtual flow node used by the list view. The thread starts when a drag over is detected “close” to the top or bottom edges. “Close” is defined by a proximity variable.
This code can obviously be improved by using a property for the proximity value and the types “Task” and “Service” for the threading work.
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | package com.dlsc; import javafx.application.Platform; import javafx.scene.Node; import javafx.scene.control.ListView; import javafx.scene.input.ClipboardContent; import javafx.scene.input.DragEvent; import javafx.scene.input.Dragboard; import javafx.scene.input.MouseEvent; import javafx.scene.input.TransferMode; import javafx.scene.layout.Region; /* * Yes, unfortunately we need to use private API for this. */ import com.sun.javafx.scene.control.skin.VirtualFlow; public class AutoscrollListView<T> extends ListView<T> { final double proximity = 20 ; public AutoscrollListView() { addEventFilter(MouseEvent.DRAG_DETECTED, evt -> startDrag()); addEventFilter(DragEvent.DRAG_OVER, evt -> autoscrollIfNeeded(evt)); addEventFilter(DragEvent.DRAG_EXITED, evt -> stopAutoScrollIfNeeded(evt)); addEventFilter(DragEvent.DRAG_DROPPED, evt -> stopAutoScrollIfNeeded(evt)); addEventFilter(DragEvent.DRAG_DONE, evt -> stopAutoScrollIfNeeded(evt)); } private void startDrag() { Dragboard db = startDragAndDrop(TransferMode.MOVE); ClipboardContent content = new ClipboardContent(); /* * We have to add some content, otherwise drag over * will not be called. */ content.putString( "dummy" ); db.setContent(content); } private void autoscrollIfNeeded(DragEvent evt) { evt.acceptTransferModes(TransferMode.ANY); /* * Determine the "hot" region that will trigger automatic scrolling. * Ideally we use the clipped container of the list view skin but when * the rows are empty the dimensions of the clipped container will be * 0x0. In this case we try to use the virtual flow. */ Region hotRegion = getClippedContainer(); if (hotRegion.getBoundsInLocal().getWidth() < 1 ) { hotRegion = this ; if (hotRegion.getBoundsInLocal().getWidth() < 1 ) { stopAutoScrollIfNeeded(evt); return ; } } double yOffset = 0 ; // y offset double delta = evt.getSceneY() - hotRegion.localToScene( 0 , 0 ).getY(); if (delta < proximity) { yOffset = -(proximity - delta); } delta = hotRegion.localToScene( 0 , 0 ).getY() + hotRegion.getHeight() - evt.getSceneY(); if (delta < proximity) { yOffset = proximity - delta; } if (yOffset != 0 ) { autoscroll(yOffset); } else { stopAutoScrollIfNeeded(evt); } } private VirtualFlow<?> getVirtualFlow() { return (VirtualFlow<?>) lookup( "VirtualFlow" ); } private Region getClippedContainer() { /* * Safest way to find the clipped container. lookup() does not work at * all. */ for (Node child : getVirtualFlow().getChildrenUnmodifiable()) { if (child.getStyleClass(). contains( "clipped-container" )) { return (Region) child; } } return null ; } class ScrollThread extends Thread { private boolean running = true ; private double yOffset; public ScrollThread() { super ( "Autoscrolling List View" ); setDaemon( true ); } @Override public void run() { /* * Some initial delay, especially useful when * dragging something in from the outside. */ try { Thread.sleep( 300 ); } catch (InterruptedException e1) { e1.printStackTrace(); } while (running) { Platform.runLater(() -> { scrollY(); }); try { sleep( 15 ); } catch (InterruptedException e) { e.printStackTrace(); } } } private void scrollY() { VirtualFlow<?> flow = getVirtualFlow(); flow.adjustPixels(yOffset); } public void stopRunning() { this .running = false ; } public void setDelta( double yOffset) { this .yOffset = yOffset; } } private ScrollThread scrollThread; private void autoscroll( double yOffset) { if (scrollThread == null ) { scrollThread = new ScrollThread(); scrollThread.start(); } scrollThread.setDelta(yOffset); } private void stopAutoScrollIfNeeded(DragEvent evt) { if (scrollThread != null ) { scrollThread.stopRunning(); scrollThread = null ; } } } |
Reference: | JavaFX Tip 15: ListView Autoscrolling from our JCG partner Dirk Lemmermann at the Pixel Perfect blog. |