当前位置:网站首页>为JTable添加按列选择功能
为JTable添加按列选择功能
2022-07-21 11:25:00 【小莫M】
用Swing的童鞋都知道,JTable在单击某一列的首部时只能达成排序的效果,而不能以此选择整列的内容。而用惯Excel表格的童鞋是否希望JTable能按列选择呢?下面就做一个可以按列选择的JTable。
要向JTable中添加按列选择功能,很简单,只需完成下面两个工作即可:
- 修改选择方式。
- 添加一个鼠标监听事件MouseListener。
先看看代码:
只实现了JTable 中常用的构造方法,其他类推。JTable默认的是按行选择,我们用17、19行代码实现按列选择并屏蔽按行选择。接下来就是要捕获对首部的鼠标单击事件。JTableHeader不含addActionListener()方法,只能为其添加一个MouseLinstener监听器了并重写它的mouseReleased()方法。
25行判断Shift键是否被按下,按下则是希望多选,把这一列添加到已选内容中;若未按下,则取消所有选择,重新选。27行获得当前单击的是哪一列。28行获得当前列搞定。
测试类如下:
效果如图所示:
边栏推荐
- A solution for adding new MySQL users that cannot be authorized
- Li Shuo, vice president of Baidu: AI can go deep into the scene and create real value. From sensors to large screens, it is just the beginning of digitalization
- jdbc批量插入10万/100万条数据
- 局部内部类
- 3.3栈和队列的应用
- 3.2队列
- 国产数据库的春天里
- MySQL终章
- 网站为什么会有漏洞要去修补
- 从浏览器上传、下载Excel文件到数据库
猜你喜欢
Compilation Principle Experiment 1 -- principle and implementation of lexical analysis program design
Lorsque idea crée un dossier, le dossier vide du dossier s'agrandit et se chevauche
PostgreSQL初/中/高级认证考试(7.16)通过考生公示
Li Shuo, vice president of Baidu: AI can go deep into the scene and create real value. From sensors to large screens, it is just the beginning of digitalization
网站为什么会有漏洞要去修补
Field injection is not recommended怎么处理
Talking about the application of service worker in caching resources and web push
2.3线性表的链式表示(1)
When servlet writes webapp, filter interception is used to realize login verification
MONAI Label 安装流程及使用攻略
随机推荐
压缩字符串
Sequences, Time Series and Prediction in Tessorflow quizs on Coursera (一)
[39题] 牛客深度学习专项题
PostgreSQL每周新闻—2022年7月13日
从0开始实现一个代理池
Tcl编程风格指南
servlet写webapp时,用filter拦截实现登陆验证
读《读大学,究竟读什么》读后感
线程理论知识
Xiangtan party and government delegation visited Qilin Xin'an for investigation
February day
Application of DC distribution system products at the end of data center
秒杀设计
Date tool class
二月天
关于Thread.sleep()方法
Lorsque idea crée un dossier, le dossier vide du dossier s'agrandit et se chevauche
JS高级 之 ES5 实现继承
eBPF验证器
Local inner class