Java 想要从 JList 获取选定的索引。但返回-1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18305516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wants to get selected index fron a JList. But returns -1
提问by RSST
I made a jlist and set a default list model using setModel method. When I called getSelectedIndex(), it returns -1. Why is it?
我制作了一个 jlist 并使用 setModel 方法设置了一个默认列表模型。当我调用 getSelectedIndex() 时,它返回 -1。为什么?
if (!listAdded.isSelectionEmpty()) {
listModelAdded.removeElementAt(listAdded.getSelectedIndex());
listBankQuestions.remove(listAdded.getSelectedIndex());
System.out.println(i);
--i;
System.out.println("Selected : " + listAdded.getSelectedIndex());
}
采纳答案by Prasad Kharkar
The method getSelectedIndex()
returns -1
if no item is selected
如果未选择任何项目,则该方法getSelectedIndex()
返回-1
回答by Stephen C
The javadoc clearly states that getSelectedIndex()
returns -1 if no item is selected.
javadoc 明确指出,getSelectedIndex()
如果未选择任何项目,则返回 -1。
If you are seeing -1
when you believe that you shouldn't, then I can think of three explanations:
如果您-1
在认为不应该看到时看到,那么我可以想到三种解释:
Something else (in your code) is removing the selection beforethe
getSelectedIndex()
call happens.You are accessing the Swing data structures incorrectly. If you try to access them from some other thread than the event listener thread, there is no guarantee that it will see the correct state.
You are somehow misreading the symptoms; e.g. you are calling
getSelectedIndex()
on the wrong object.
其他的东西(在你的代码)被删除的选择之前的
getSelectedIndex()
调用发生。您正在错误地访问 Swing 数据结构。如果您尝试从事件侦听器线程之外的某个其他线程访问它们,则无法保证它会看到正确的状态。
你不知何故误读了症状;例如,您调用
getSelectedIndex()
了错误的对象。
(A final possibility is "its a Swing bug" ... but before anyone could make that call, you'd need to provide a proper SSCCE that demonstrates the problem. And frankly, I think it is highly unlikely that this really is a Swing bug.)
(最后一种可能性是“它是一个 Swing 错误”……但在任何人都可以拨打该电话之前,您需要提供一个适当的 SSCCE 来证明该问题。坦率地说,我认为这真的不太可能是一个摆动错误。)
回答by EsmaeelQash
Because you removed the selected item, then there is no selection (selection Removed) and the return value is -1
因为你去掉了选中项,那么就没有选中(selection Removed),返回值为-1
回答by Flip
Personally, I had to use .locationToIndex because .getSelectedIndex wasn't working correctly with right-clicking, returning -1. Here's some working code:
就个人而言,我不得不使用 .locationToIndex,因为 .getSelectedIndex 无法通过右键单击正常工作,返回 -1。这是一些工作代码:
tileSelector.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
// get Selected Tile
int idx = tileSelector.locationToIndex(e.getPoint());
tileSelector.setSelectedIndex(idx);
if (SwingUtilities.isLeftMouseButton(e)) {
debugOutput.append(idx + " Left Click\n");
debugOutput.setCaretPosition(debugOutput.getDocument().getLength());
} else if (SwingUtilities.isRightMouseButton(e)) {
debugOutput.append(idx + " Right Click\n");
debugOutput.setCaretPosition(debugOutput.getDocument().getLength());
}
}
});