没有合适的资源?快使用搜索试试~ 我知道了~
首页Java Swing中JList选择事件监听器ListSelectionListener用法示例
资源详情
资源评论
资源推荐
Java Swing中中JList选择事件监听器选择事件监听器ListSelectionListener用用
法示例法示例
主要介绍了Java Swing中JList选择事件监听器ListSelectionListener用法,结合具体实例形式分析了中JList选择事
件监听器ListSelectionListener的功能、使用方法及相关注意事项,需要的朋友可以参考下
本文实例讲述了Java Swing中JList选择事件监听器ListSelectionListener用法。分享给大家供大家参考,具体如下:
当JList中的元素被选中时,选择事件将被触发。对于JTable也是一样,你可以把它看做是多个并列的JList。那么,如果程序
需要对该事件做出响应,需要以下步骤:
(1)创建一个实现了 ListSelectionListener的监听器;
(2)使用JList或selectionModel的addListSelectionListener添加监听器;
(3)在监听器的valueChanged方法添加响应代码。
在响应代码中需要注意的是getValueIsAdjusting值的判断。测试表明,每当我们进行选择时,valueChanged方法都会被激活
多次,其中,在最后的鼠标操作中,getValueIsAdjusting值为false,而在一系列中间操作中,该值均为true。比如说,用鼠标
连续划过一串元素时,会有一系列getValueIsAdjusting为true的valueChanged方法激活,且最后一次为false。而我们对选择
事件的判定一般是以最后接触为准,因此这里对getValueIsAdjusting值进行一个判断。
常用方法如下:
getLeadSelectionIndex()
返回当前选中的元素的index。
getMinSelectionIndex()
返回选中的多个元素中index的最小值,如果选择为空在返回-1。
getMaxSelectionIndex()
原理同上。
isSelectedIndex(int index)
判断指定index是否被选中。
clearSelection()
清除选中。
getSelectedIndex()
返回被选中的所有元素中最小的index。
getSelectedIndices()
返回一个整型数组,包含被选中的所有index。
getSelectedValue()
返回被选中的,index最小的元素值。
getSelectedValues()
返回一个object数组,包含被选中的所有元素对象。
getSelectedValuesList()
返回一个objectList,包含被选中的所有元素对象。
下面的demo来自于ListSelectionDemo.java
/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
weixin_38688969
- 粉丝: 3
- 资源: 939
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论10