JavaFX2: 鼠标拖动选择和Ctrl+Shift连续区间选择的ListView
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的更多相关文章
- Ctrl+Shift+F12切换最大化编辑器
常用快捷键(keymaps:Default情况下) Esc键编辑器(从工具窗口) F1帮助千万别按,很卡! F2(Shift+F2)下/上高亮错误或警告快速定位 F3向下查找关键字出现位置 F4查找变 ...
- (转)Eclipse快捷键大全,导包快捷键:ctrl+Shift+/
Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...
- Eclipse快捷键大全,导包快捷键:ctrl+Shift+/【转】
Ctrl+Shift+L 显示所有快捷键 Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复 ...
- CTRL+SHIFT
CTRL+SHIFT+鼠标左右,上下拖动,可快速实现平行和垂直上下复制的功能,
- 【editplus经常用的快捷键】Editplus 选中一行ctrl+r,Edit 合并行 Ctrl+Shift+J 合并选定行 删除当前行
Editplus 选中一行: ctrl+rEditplus 复制一行: ctrl+r选择行,然后ctrl+c复制.复制一行到下一行中:Editplus有:Ctrl+j 复制上一行的一个字符到当前行Ed ...
- echats 油表盘 鼠标拖动指针改变数值
近期需要做一个鼠标拖动完成油表盘数值改变的功能,使用canvas感觉太麻烦,而且指针不太好监听和拖动,只能另谋出路,在网上参考了某位大神的操作,最终选择了echats来解决这个问题.废话不多说,直接上 ...
- python模拟鼠标拖动操作的方法
本文实例讲述了python模拟鼠标拖动操作的方法.分享给大家供大家参考.具体如下: pdf中的书签只有页码,准备把现有书签拖到一个目录中,然后添加自己页签.重复的拖动工作实在无趣,还是让程序帮我实现吧 ...
- listbox鼠标拖动数据和为button注册快捷键
将listbox1中的数据用鼠标拖动至listbox2,即有左至右. 分别对应控件注册如下事件DragEnter,MouseDown,DragDrop 代码如下: //P128 DataGridVie ...
- Android Studio 调试过程中快捷查看断点处变量值(Ctrl+Shift+I无效)?
当你在做Keymap到Eclipse后,在debug过程中,在Eclipse中我们很喜欢用Ctrl+Shift+I去查看一个运算或者调用的结果,这样用起来很方便.但是keymap到Eclipse后,你 ...
随机推荐
- 设计模式(Abstract Factory)抽象工厂
1. 需求: 设计一个电脑组装程序,对于组装品牌电脑. 用零件组装(主板.硬盘.显示器)由品牌提供的所有. 让我们组装一台联想电脑,板子.由联想提供. (眼下仅仅有Lenovo和Dell两种品牌) 2 ...
- VC实现URL编解码器
//变化UTF8为了中国 void UTF8ToGB(CString& szstr) { WCHAR* strSrc; TCHAR* szRes; int i = MultiByteToWid ...
- 在VS中实现webService的一个demo(图解)
在VS中实现webService的一个demo(图解) 先创建一个web项目,创建好web项目后,添加新建项——web服务 在新建好的web服务文件中写如下代码: 生成当前解决方案. 新建一个winf ...
- cocos2d-x环境的搭建之xcode-本人亲历成功搭建!
cocos2d-x环境的搭建之xcode-本人亲历成功搭建! 写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不管是简单的还是困难的现在都慢慢的一步一步克服了, ...
- 关于如何实现程序一天只启动一次的想法(C++实现)
问题描述: 我们在程序开发当中,经常会遇到某些子程序需要实现一天只启动一次的功能,该功能实现的方法有很多种,其原理都是通过记录标记为来实现的.本次要分享的也是利用程序标记为来实现的,而且只需要使用一个 ...
- Python,PIL压缩裁剪图片
自己写了用来压缩 DC 照片的,批量处理整目录文件,非常方便.需要安装 PIL #!/usr/bin/env python import Image import os import os.path ...
- 基于visual Studio2013解决C语言竞赛题之1028平均值
题目 解决代码及点评 /* 已知有9个数,请求出这些数中的最大值.最小值及平均值,以及有多少个数等于平均值? */ #include<stdio.h> ...
- Cppcheck 1.54 C/C++静态代码分析工具
Cppcheck是一个C/C++代码分析工具,只检测那些编译器通常无法检测到的bug类型. 官方上建议让编译器提供尽量多的警告提示:1.使用Visual C++的话,应使用警告等级4 2.使用GC ...
- Java ArrayList add(int index, E element) example
Simple add() method is used for adding an element at the end of the list however there is another va ...
- 删除PO
FUNCTION zrfc_mm002. *"---------------------------------------------------------------------- * ...