ListSelectionListener 中的 valueChanged 不起作用

发布于 2024-09-24 22:04:05 字数 1378 浏览 0 评论 0原文

我创建了以下实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赠佳期 2024-10-01 22:04:05

我阅读的文档表明 ListSelectionEvent 仅告诉您 firstIndexlastIndex 之间的选择已更改,但没有告诉您朝哪个方向更改。一旦您知道发生了更改(已触发 ListSelectionEvent),您就可以从 JList 中读取当前选定的值:

selected_row = ((JList) e.getSource()).getSelectedIndex();

您需要检查 selected_row 是非负数,以防用户操作只是取消选择唯一选定的选项。

I read the docs as indicating that the ListSelectionEvent only tells you that the selection between firstIndex and lastIndex was changed, but not in which direction. Once you know that a change has occurred (that a ListSelectionEvent has been fired) you could just read the current selected value from the JList:

selected_row = ((JList) e.getSource()).getSelectedIndex();

You'll want to check selected_row is non-negative, in case the user operation is simply unselecting the only selected option.

冰雪之触 2024-10-01 22:04:05

您能否分享将此侦听器附加到 JList 的代码?
它应该类似于:

list = new JList(listData);
listSelectionModel = list.getSelectionModel();
listSelectionModel.addListSelectionListener(
         new FormatListSelectionListener());

请参阅如何编写ListSelection Listener

Can you please share the code that attaches this listener the the JList?
It should be something like:

list = new JList(listData);
listSelectionModel = list.getSelectionModel();
listSelectionModel.addListSelectionListener(
         new FormatListSelectionListener());

See How to write ListSelection Listener

洛阳烟雨空心柳 2024-10-01 22:04:05

您不想检查 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文