有人问过一个非常相似的问题,虽然我承认,但问题的解决方案并不能完全解决我的问题。单击JList
时,将选择与鼠标单击最接近的索引列表项。 JList
也会在每次点击+拖动事件触发时执行此操作。
当单击+拖动位置在可见列表之外时,我想防止JList
在单击+拖动事件期间选择项目。我将如何处理?
我曾考虑过覆盖另一种方法,该方法涉及选择列表项的click + drag事件。我想尝试setSelectionInterval()
方法。
JList<String list = new JList<String>(){
private static final long serialVersionUID = 1L;
@Override
public int locationToIndex(Point location) {
int index = super.locationToIndex(location);
if (index != -1 && !getCellBounds(index, index).contains(location)) {
clearSelection();
return -1;
//an excellent click-only solution to prohibit the selecting of
//items from beyond the visible list
}
else {
return index;
}
}
@Override
public void setSelectionInterval(int anchor, int lead) {
super.setSelectionInterval(anchor, lead);
System.out.println("setSelectionInterval");
}
};
我发现,每次单击并拖动所显示的
JList
上的任意位置,都会得到我添加到上述方法中的“ setSelectionInterval”的System.out消息。就覆盖方法而言,我不知道从何而来。也许那不是我应该如何处理的。在setSelectionInterval()
的源代码中,我迷失了试图找到通往所涉及的任何侦听器的方法,因此来到了这里。 :p我非常感谢任何指向我应该寻找的地方或解决方案的指针。提前致谢!
这是一个SSCCE示例,与我的设置非常接近。照原样,当单击事件从列表项本身触发时,列表将不会选择项。我希望从列表项中触发click + drag事件时也会发生同样的效果。
import java.awt.BorderLayout;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
public class TestMain {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel content = new JPanel(new BorderLayout());
String[] data = {"Luke", "Jeff", "Bryce"};
JList<String> list = new JList<String>(data){
private static final long serialVersionUID = 1L;
@Override
public int locationToIndex(Point location) {
System.out.println("location to index");
int index = super.locationToIndex(location);
if (index != -1 && !getCellBounds(index, index).contains(location)) {
clearSelection();
return -1;
}
else {
return index;
}
}
}
content.add(list, BorderLayout.CENTER);
frame.setContentPane(content);
frame.setSize(200,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
最佳答案
如果禁用整个拖动功能是可以接受的,则以下内容应会有所帮助:
@Override
protected void processMouseMotionEvent(MouseEvent e) {
if(MouseEvent.MOUSE_DRAGGED != e.getID()) {
super.processMouseMotionEvent(e);
}
}