ListSelectionListener 中的 valueChanged 不起作用
我创建了以下实现 ListSelectionListener 接口的类。这个类应该“监听”我创建的 JList 的选择事件。每次用户单击此列表中的一行时,selected_row 值都应该更新,并且字符串“选择的格式行是...”应该因此更改。但是,多次单击行后,select_row 值不会更改。有人能为我提供一个解释,并希望能提供一种做我想做的事情的方法吗?提前致谢!!
import java.util.List;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import ee.dobax.portal.CommonPath;
public class FormatListSelectionListener implements ListSelectionListener{
public ContentGenerated content;
private CommonPathList path_list;
private ConfigRenderingDialog dialog;
public FormatListSelectionListener(ConfigRenderingDialog dialog){
content = dialog.content;
path_list = dialog.pathList;
}
public void valueChanged(ListSelectionEvent e) {
int selected_row;
if(e.getValueIsAdjusting() == false){
selected_row = e.getLastIndex();
System.out.println("The format row selected is "+selected_row);
path_list.addFormatListRowSelected(selected_row);
List<CommonPath> list_p = content.getPathList(selected_row);
Object[] path_list_to_array = new Object[list_p.size()];
path_list.getContents().removeAllElements();
for(int x = 0; x < list_p.size(); x++){
path_list_to_array[x] = list_p.get(x);
path_list.getContents().addElement(path_list_to_array[x]);
}
}
}
}
I have created the following class implementing a ListSelectionListener interface. This class should "listen" to the selection events of a JList I have created. Everytime the use clicks on a row of this list, the selected_row value should be updated and the string "The format row selected is ...." should therefore change. However after clicking the rows more than once, the select_row value doesn't change. Can anybody provide me a with an explanation for this and hopefully, a way to do what I want? Thanks in advance!!
import java.util.List;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import ee.dobax.portal.CommonPath;
public class FormatListSelectionListener implements ListSelectionListener{
public ContentGenerated content;
private CommonPathList path_list;
private ConfigRenderingDialog dialog;
public FormatListSelectionListener(ConfigRenderingDialog dialog){
content = dialog.content;
path_list = dialog.pathList;
}
public void valueChanged(ListSelectionEvent e) {
int selected_row;
if(e.getValueIsAdjusting() == false){
selected_row = e.getLastIndex();
System.out.println("The format row selected is "+selected_row);
path_list.addFormatListRowSelected(selected_row);
List<CommonPath> list_p = content.getPathList(selected_row);
Object[] path_list_to_array = new Object[list_p.size()];
path_list.getContents().removeAllElements();
for(int x = 0; x < list_p.size(); x++){
path_list_to_array[x] = list_p.get(x);
path_list.getContents().addElement(path_list_to_array[x]);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我阅读的文档表明
ListSelectionEvent
仅告诉您firstIndex
和lastIndex
之间的选择已更改,但没有告诉您朝哪个方向更改。一旦您知道发生了更改(已触发ListSelectionEvent
),您就可以从JList
中读取当前选定的值:您需要检查
selected_row
是非负数,以防用户操作只是取消选择唯一选定的选项。I read the docs as indicating that the
ListSelectionEvent
only tells you that the selection betweenfirstIndex
andlastIndex
was changed, but not in which direction. Once you know that a change has occurred (that aListSelectionEvent
has been fired) you could just read the current selected value from theJList
:You'll want to check
selected_row
is non-negative, in case the user operation is simply unselecting the only selected option.您能否分享将此侦听器附加到 JList 的代码?
它应该类似于:
请参阅如何编写ListSelection Listener
Can you please share the code that attaches this listener the the JList?
It should be something like:
See How to write ListSelection Listener
您不想检查 e.getValueIsAdjusting() 是否为真吗?因为那应该意味着事件发生了变化。这可能就是为什么它可以工作一次(第一次可能没有变化),然后就不起作用了。
另外,我会将其更改为 if(e.getValueIsAdjusting()) 因为它返回一个布尔值。
Wouldn't you want to check for e.getValueIsAdjusting() being true? Because that should mean that the event changed. This is probably why it works once (the first time there may be no change) and after that doesn't work.
Also I'd change it to say if(e.getValueIsAdjusting()) as it returns a boolean value.