JList

编程入门 行业动态 更新时间:2024-10-27 08:27:57
本文介绍了JList - 单击已选择的项目时取消选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果单击JList上的选定索引,我希望它取消选择。换句话说,点击索引实际上会切换他们的选择。看起来不支持,所以我尝试了

If a selected index on a JList is clicked, I want it to de-select. In other words, clicking on the indices actually toggles their selection. Didn't look like this was supported, so I tried

list.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { java.awt.Point point = evt.getPoint(); int index = list.locationToIndex(point); if (list.isSelectedIndex(index)) list.removeSelectionInterval(index, index); } });

这里的问题是在 JList已经采取行动之后正在调用它在鼠标事件上,所以它取消选择一切。然后我尝试删除所有JList的MouseListeners,添加我自己的,然后添加所有默认侦听器。这不起作用,因为在我取消选择后,JList会重新选择索引。无论如何,我最终提出的是

The problem here is that this is being invoked after JList has already acted on the mouse event, so it deselects everything. So then I tried removing all of JList's MouseListeners, adding my own, and then adding all of the default listeners back. That didn't work, since JList would reselect the index after I had deselected it. Anyway, what I eventually came up with is

MouseListener[] mls = list.getMouseListeners(); for (MouseListener ml : mls) list.removeMouseListener(ml); list.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent evt) { java.awt.Point point = evt.getPoint(); final int index = list.locationToIndex(point); if (list.isSelectedIndex(index)) SwingUtilities.invokeLater(new Runnable() { public void run() { list.removeSelectionInterval(index, index); } }); } }); for (MouseListener ml : mls) list.addMouseListener(ml);

......这是有效的。但我不喜欢它。有更好的方法吗?

... and that works. But I don't like it. Is there a better way?

推荐答案

在此处查看示例ListSelectionModel:启用切换选择模式: java.sun/products/jfc/tsc/tech_topics/jlist_1/jlist .html

Looking at the Example "ListSelectionModel: Enabling Toggle Selection Mode" here: java.sun/products/jfc/tsc/tech_topics/jlist_1/jlist.html

我已经为多选列表框略微修改了它(将setSelectionInterval更改为addSelectionInterval)并且如果你点击到它就消除了重新选择的问题在鼠标停止时取消选择并移动鼠标(移动gestureStarted检查添加和删除)。

I have modified it slightly for multi-select list boxes (changed setSelectionInterval to addSelectionInterval) and eliminated a problem with re-selection if you click to de-select and move your mouse while the mouse is down (moved the gestureStarted check for both add and remove).

objList.setSelectionModel(new DefaultListSelectionModel() { private static final long serialVersionUID = 1L; boolean gestureStarted = false; @Override public void setSelectionInterval(int index0, int index1) { if(!gestureStarted){ if (isSelectedIndex(index0)) { super.removeSelectionInterval(index0, index1); } else { super.addSelectionInterval(index0, index1); } } gestureStarted = true; } @Override public void setValueIsAdjusting(boolean isAdjusting) { if (isAdjusting == false) { gestureStarted = false; } } });

更多推荐

JList

本文发布于:2023-08-06 05:14:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310278.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!