当前位置:   article > 正文

JavaFX2: 鼠标拖动选择和Ctrl+Shift连续区间选择的ListView_javafx listview 按ctrl多选

javafx listview 按ctrl多选

JavaFX2的ListView中的多选没有提供鼠标拖动选择的功能,同时按下Ctrl和Shift后连续的区间选中也不支持,以下代码用于处理这两个问题,细节见代码注释:

 

  1. import com.sun.javafx.scene.control.skin.ListViewSkin;
  2. import com.sun.javafx.scene.control.skin.VirtualFlow;
  3. import javafx.collections.FXCollections;
  4. import javafx.collections.ObservableList;
  5. import javafx.event.EventHandler;
  6. import javafx.scene.control.IndexedCell;
  7. import javafx.scene.control.ListView;
  8. import javafx.scene.control.SelectionMode;
  9. import javafx.scene.input.MouseEvent;
  10. /**
  11. * 该类增强了ListView本身的行中选中功能.
  12. * <br/>1. 鼠标拖动选中
  13. * <br/>2. 连续Ctrl+Shift区间选中
  14. *
  15. * 其中使用VirtualFlow vf = ((VirtualFlow) ((ListViewSkin)
  16. * getChildrenUnmodifiable().get(0)).getChildrenUnmodifiable().get(0));来判断当前显示可见的行号,
  17. * 使用setOnMousePressed/setOnMouseDragged/setOnMouseReleased来处理增强的拖动和Ctrl+Shift选中事件.
  18. *
  19. * 遗留问题: 当Ctrl+Shift操作后一次鼠标点击在一个已经选中的行时, 最后的结果会取消选中该行.
  20. * 如果还需要添加其他鼠标事件而需要使用到选中状态时可能会有冲突, 还未测试.
  21. *
  22. * @author Alan Zeng
  23. */
  24. public class DragSelectionListView<T extends Object> extends ListView<T> {
  25. /**
  26. * 鼠标拖动之前ListView的选中状态. 在鼠标拖动的过程中需要根据拖动事件的起始行号和当前行号来计算新选中的行,
  27. * 同事和原始选中状态结合作为新的选中状态.
  28. */
  29. private ObservableList<Integer> oldSelectedIndices;
  30. /**
  31. * 鼠标拖动事件是否已经开始. 会在MouseDragged中设置为true, 在MouseReleased中重置为false
  32. */
  33. private boolean isDragStarted = false;
  34. /**
  35. * 最后一次鼠标点击选中的行号. 每次鼠标点击时都会进行记录
  36. */
  37. private int lastPressedRow;
  38. /**
  39. * 鼠标拖动事件的起始行. 会在MousePressed中设置为当前点击行, 在MouseReleased中重置为-1
  40. */
  41. private int dragStartedRow = -1;
  42. /**
  43. * 上一次拖动经过的行号. 鼠标拖动事件过程中, 会不断的触发MouseDragged事件, 每次事件结束时记录鼠标所在行号,
  44. * 在MouseReleased中重置为-1
  45. */
  46. private int prevDragRow = -1;
  47. public DragSelectionListView() {
  48. getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  49. addDragSelectionEventHandlers();
  50. }
  51. public DragSelectionListView(ObservableList<T> ol) {
  52. super(ol);
  53. getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  54. addDragSelectionEventHandlers();
  55. }
  56. /**
  57. * 根据相对于ListView的坐标,获取鼠标所在行
  58. *
  59. * @param x
  60. * @param y
  61. * @return
  62. */
  63. public int getRowAtPoint(double x, double y) {
  64. int row = -1;
  65. VirtualFlow vf = ((VirtualFlow) ((ListViewSkin) getChildrenUnmodifiable().get(0)).getChildrenUnmodifiable().get(0));
  66. int firstIndex = vf.getFirstVisibleCell().getIndex();
  67. int lastIndex = vf.getLastVisibleCell().getIndex();
  68. for (int i = firstIndex; i <= lastIndex; i++) {
  69. IndexedCell visibleCell = vf.getVisibleCell(i);
  70. if (visibleCell.getBoundsInParent().contains(x, y)) {
  71. row = i;
  72. break;
  73. }
  74. }
  75. return row;
  76. }
  77. /**
  78. * 获取当前显示出来的第一行行号
  79. *
  80. * @return
  81. */
  82. public int getFirstVisibleRow() {
  83. VirtualFlow vf = ((VirtualFlow) ((ListViewSkin) getChildrenUnmodifiable().get(0)).getChildrenUnmodifiable().get(0));
  84. return vf.getFirstVisibleCell().getIndex();
  85. }
  86. /**
  87. * 获取当前显示出来的最后一行行号
  88. *
  89. * @return
  90. */
  91. public int getLastVisibleRow() {
  92. VirtualFlow vf = ((VirtualFlow) ((ListViewSkin) getChildrenUnmodifiable().get(0)).getChildrenUnmodifiable().get(0));
  93. return vf.getLastVisibleCell().getIndex();
  94. }
  95. /**
  96. * 添加用于处理拖动选中和连续Ctrl+Shift选中的事件: MousePressed/MouseDraggedMouseReleased
  97. */
  98. private void addDragSelectionEventHandlers() {
  99. setOnMousePressed(new EventHandler<MouseEvent>() {
  100. @Override
  101. public void handle(MouseEvent t) {
  102. final int rowAtPoint = getRowAtPoint(t.getX(), t.getY());
  103. //<editor-fold defaultstate="collapsed" desc="当Shift和Ctrl键同时按下时,会增加选中鼠标两次点击之间的行(不分左右键)">
  104. if (t.isControlDown() && t.isShiftDown()) {
  105. final int min = Math.min(rowAtPoint, lastPressedRow);
  106. final int max = Math.max(rowAtPoint, lastPressedRow);
  107. DragSelectionListView.this.getSelectionModel().selectRange(min, max + 1);
  108. }
  109. //</editor-fold>
  110. dragStartedRow = rowAtPoint;
  111. lastPressedRow = rowAtPoint;
  112. }
  113. });
  114. setOnMouseDragged(new EventHandler<MouseEvent>() {
  115. @Override
  116. public void handle(MouseEvent t) {
  117. int rowAtPoint = getRowAtPoint(t.getX(), t.getY());
  118. if (prevDragRow == rowAtPoint) {
  119. return;
  120. }
  121. ObservableList<Integer> selectedIndices = DragSelectionListView.this.getSelectionModel().getSelectedIndices();
  122. if (!isDragStarted) {
  123. oldSelectedIndices = FXCollections.observableArrayList(selectedIndices);
  124. isDragStarted = true;
  125. } else {
  126. DragSelectionListView.this.getSelectionModel().clearSelection();
  127. for (Integer integer : oldSelectedIndices) {
  128. DragSelectionListView.this.getSelectionModel().selectIndices(integer);
  129. }
  130. if (dragStartedRow != -1) {
  131. DragSelectionListView.this.getSelectionModel().selectRange(Math.min(rowAtPoint, dragStartedRow), Math.max(rowAtPoint, dragStartedRow) + 1);
  132. }
  133. }
  134. prevDragRow = rowAtPoint;
  135. }
  136. });
  137. setOnMouseReleased(new EventHandler<MouseEvent>() {
  138. @Override
  139. public void handle(MouseEvent t) {
  140. //下面主要是重置Drag完毕后的一些状态
  141. dragStartedRow = -1;
  142. prevDragRow = -1;
  143. isDragStarted = false;
  144. }
  145. });
  146. }
  147. }


 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/191716
推荐阅读
相关标签
  

闽ICP备14008679号