加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 通过单击位置从JList获取组件

发布时间:2020-12-14 23:46:00 所属栏目:Java 来源:网络整理
导读:如何通过单击位置从JList获取组件? 我有自己的列表单元格渲染器,我插入一些面板和标签. 现在我想得到,例如用户点击的标签. 我尝试了方法list.getComponentAt(evt.getPoint());但它只返回整个JList. 解决方法 我没有测试过这个,但基础知识是…… 使用JList#l
如何通过单击位置从JList获取组件?

我有自己的列表单元格渲染器,我插入一些面板和标签.
现在我想得到,例如用户点击的标签.

我尝试了方法list.getComponentAt(evt.getPoint());但它只返回整个JList.

解决方法

我没有测试过这个,但基础知识是……

>使用JList#locationToIndex(Point)获取元素的索引
给定的点.
>在指定的索引处获取“元素”(使用
JList的#getModel#getElementAt(INT)).
>使用JList#getCellRenderer获取ListCellRenderer.
>渲染元素并获取它的Component表示
>将渲染器的边界设置为所需的单元格边界
>将原始Point转换为Components上下文
>在渲染器上使用getComponentAt …

可能,像……

int index = list.locationToIndex(p);
Object value = list.getModel().getElementAt(int);
Component comp = listCellRenderer.getListCellRendererComponent(list,value,index,true,true);
comp.setBounds(list.getCellBounds(index,index));
Point contextPoint = SwingUtilities.convertPoint(list,p,comp);
Component child = comp.getComponentAt(contextPoint);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读