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

import com.sun.javafx.scene.control.skin.ListViewSkin;
import com.sun.javafx.scene.control.skin.VirtualFlow;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.IndexedCell;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.input.MouseEvent; /**
* 该类增强了ListView本身的行中选中功能.
* <br/>1. 鼠标拖动选中
* <br/>2. 连续Ctrl+Shift区间选中
*
* 其中使用VirtualFlow vf = ((VirtualFlow) ((ListViewSkin)
* getChildrenUnmodifiable().get(0)).getChildrenUnmodifiable().get(0));来判断当前显示可见的行号,
* 使用setOnMousePressed/setOnMouseDragged/setOnMouseReleased来处理增强的拖动和Ctrl+Shift选中事件.
*
* 遗留问题: 当Ctrl+Shift操作后一次鼠标点击在一个已经选中的行时, 最后的结果会取消选中该行.
* 如果还需要添加其他鼠标事件而需要使用到选中状态时可能会有冲突, 还未测试.
*
* @author Alan Zeng
*/
public class DragSelectionListView<T extends Object> extends ListView<T> { /**
* 鼠标拖动之前ListView的选中状态. 在鼠标拖动的过程中需要根据拖动事件的起始行号和当前行号来计算新选中的行,
* 同事和原始选中状态结合作为新的选中状态.
*/
private ObservableList<Integer> oldSelectedIndices;
/**
* 鼠标拖动事件是否已经开始. 会在MouseDragged中设置为true, 在MouseReleased中重置为false
*/
private boolean isDragStarted = false;
/**
* 最后一次鼠标点击选中的行号. 每次鼠标点击时都会进行记录
*/
private int lastPressedRow;
/**
* 鼠标拖动事件的起始行. 会在MousePressed中设置为当前点击行, 在MouseReleased中重置为-1
*/
private int dragStartedRow = -1;
/**
* 上一次拖动经过的行号. 鼠标拖动事件过程中, 会不断的触发MouseDragged事件, 每次事件结束时记录鼠标所在行号,
* 在MouseReleased中重置为-1
*/
private int prevDragRow = -1; public DragSelectionListView() {
getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
addDragSelectionEventHandlers();
} public DragSelectionListView(ObservableList<T> ol) {
super(ol);
getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
addDragSelectionEventHandlers();
} /**
* 根据相对于ListView的坐标,获取鼠标所在行
*
* @param x
* @param y
* @return
*/
public int getRowAtPoint(double x, double y) {
int row = -1;
VirtualFlow vf = ((VirtualFlow) ((ListViewSkin) getChildrenUnmodifiable().get(0)).getChildrenUnmodifiable().get(0));
int firstIndex = vf.getFirstVisibleCell().getIndex();
int lastIndex = vf.getLastVisibleCell().getIndex();
for (int i = firstIndex; i <= lastIndex; i++) {
IndexedCell visibleCell = vf.getVisibleCell(i);
if (visibleCell.getBoundsInParent().contains(x, y)) {
row = i;
break;
}
}
return row;
} /**
* 获取当前显示出来的第一行行号
*
* @return
*/
public int getFirstVisibleRow() {
VirtualFlow vf = ((VirtualFlow) ((ListViewSkin) getChildrenUnmodifiable().get(0)).getChildrenUnmodifiable().get(0));
return vf.getFirstVisibleCell().getIndex();
} /**
* 获取当前显示出来的最后一行行号
*
* @return
*/
public int getLastVisibleRow() {
VirtualFlow vf = ((VirtualFlow) ((ListViewSkin) getChildrenUnmodifiable().get(0)).getChildrenUnmodifiable().get(0));
return vf.getLastVisibleCell().getIndex();
} /**
* 添加用于处理拖动选中和连续Ctrl+Shift选中的事件: MousePressed/MouseDraggedMouseReleased
*/
private void addDragSelectionEventHandlers() {
setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
final int rowAtPoint = getRowAtPoint(t.getX(), t.getY()); //<editor-fold defaultstate="collapsed" desc="当Shift和Ctrl键同时按下时,会增加选中鼠标两次点击之间的行(不分左右键)">
if (t.isControlDown() && t.isShiftDown()) {
final int min = Math.min(rowAtPoint, lastPressedRow);
final int max = Math.max(rowAtPoint, lastPressedRow);
DragSelectionListView.this.getSelectionModel().selectRange(min, max + 1);
}
//</editor-fold> dragStartedRow = rowAtPoint;
lastPressedRow = rowAtPoint;
}
});
setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
int rowAtPoint = getRowAtPoint(t.getX(), t.getY());
if (prevDragRow == rowAtPoint) {
return;
} ObservableList<Integer> selectedIndices = DragSelectionListView.this.getSelectionModel().getSelectedIndices();
if (!isDragStarted) {
oldSelectedIndices = FXCollections.observableArrayList(selectedIndices);
isDragStarted = true;
} else {
DragSelectionListView.this.getSelectionModel().clearSelection();
for (Integer integer : oldSelectedIndices) {
DragSelectionListView.this.getSelectionModel().selectIndices(integer);
} if (dragStartedRow != -1) {
DragSelectionListView.this.getSelectionModel().selectRange(Math.min(rowAtPoint, dragStartedRow), Math.max(rowAtPoint, dragStartedRow) + 1);
}
} prevDragRow = rowAtPoint;
}
});
setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
//下面主要是重置Drag完毕后的一些状态
dragStartedRow = -1;
prevDragRow = -1;
isDragStarted = false;
}
});
}
}

JavaFX2: 鼠标拖动选择和Ctrl+Shift连续区间选择的ListView的更多相关文章

  1. Ctrl+Shift+F12切换最大化编辑器

    常用快捷键(keymaps:Default情况下) Esc键编辑器(从工具窗口) F1帮助千万别按,很卡! F2(Shift+F2)下/上高亮错误或警告快速定位 F3向下查找关键字出现位置 F4查找变 ...

  2. (转)Eclipse快捷键大全,导包快捷键:ctrl+Shift+/

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...

  3. Eclipse快捷键大全,导包快捷键:ctrl+Shift+/【转】

    Ctrl+Shift+L 显示所有快捷键 Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复 ...

  4. CTRL+SHIFT

    CTRL+SHIFT+鼠标左右,上下拖动,可快速实现平行和垂直上下复制的功能,

  5. 【editplus经常用的快捷键】Editplus 选中一行ctrl+r,Edit 合并行 Ctrl+Shift+J 合并选定行 删除当前行

    Editplus 选中一行: ctrl+rEditplus 复制一行: ctrl+r选择行,然后ctrl+c复制.复制一行到下一行中:Editplus有:Ctrl+j 复制上一行的一个字符到当前行Ed ...

  6. echats 油表盘 鼠标拖动指针改变数值

    近期需要做一个鼠标拖动完成油表盘数值改变的功能,使用canvas感觉太麻烦,而且指针不太好监听和拖动,只能另谋出路,在网上参考了某位大神的操作,最终选择了echats来解决这个问题.废话不多说,直接上 ...

  7. python模拟鼠标拖动操作的方法

    本文实例讲述了python模拟鼠标拖动操作的方法.分享给大家供大家参考.具体如下: pdf中的书签只有页码,准备把现有书签拖到一个目录中,然后添加自己页签.重复的拖动工作实在无趣,还是让程序帮我实现吧 ...

  8. listbox鼠标拖动数据和为button注册快捷键

    将listbox1中的数据用鼠标拖动至listbox2,即有左至右. 分别对应控件注册如下事件DragEnter,MouseDown,DragDrop 代码如下: //P128 DataGridVie ...

  9. Android Studio 调试过程中快捷查看断点处变量值(Ctrl+Shift+I无效)?

    当你在做Keymap到Eclipse后,在debug过程中,在Eclipse中我们很喜欢用Ctrl+Shift+I去查看一个运算或者调用的结果,这样用起来很方便.但是keymap到Eclipse后,你 ...

随机推荐

  1. dfs-hdu-4620-Fruit Ninja Extreme

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4620 题目大意: 切水果.给n刀,每刀的时间,每刀切的水果的种类.求能切的最多的刀数,使得每相邻的两 ...

  2. Bernstein polynomials

    Bernstein多项式能够用来一致逼近闭区间上的连续函数. 对于[0,1]上的连续函数f(x),定义Bernstein多项式 B_n(f,x) = sum{k=0..n} f(k/n)C(k,n)t ...

  3. 趋势科技4月移动client病毒报告

    2014年4月移动client安全威胁概况 截至2014年4月30日,中国区移动client病毒码1.669.60,大小9,792,484字节,能够检測病毒约221万个.移动client病毒约12万个 ...

  4. c语言,数据类型转换

    在执行算术运算时,计算机比C语言的限制更多.为了让计算机执行算术运算,通常要求操作数有相同的大小(即位的数量相同),并且要求存储的方式也相同.计算机可能可以直接将两个16位整数相加,但是不能直接将16 ...

  5. C#多线程的死锁演示

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  6. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

    问题的原因是无法找到org.slf4j.impl.StaticLoggerBinder,我找了一下,确实没有该类,网上搜了一下下面是官方的解答http://www.slf4j.org/codes.ht ...

  7. (step8.2.7)hdu 1517(A Multiplication Game——巴什博弈变形)

    题目大意:输入一个整数n.谁先报的数大于n,谁就输了.(初始值p  == 1 , 后一个人报的数必须在前一个人报的数的基础上乘上(2 ~ 9)之间的任意一个数) 解题思路:巴什博奕的变形 1) 解题思 ...

  8. C#中一些易混知识的比较

     Equals 和==的区别         C#中有两种不同的相等:引用相等和值相等         ==是比较两个变量的值是否相同或两个引用是不是指向同一个内存地址.         Equals ...

  9. 【linux】内核编译

    原创,转载时请注明,谢谢.邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http://blog.cs ...

  10. linux命令:Linux命令大全

    Linux命令大全 http://man.linuxde.net/