JList:上一个选择的项目

发布于 2025-01-03 08:54:25 字数 132 浏览 0 评论 0原文

我有一个 JList 并注册一个选择处理程序(ListSelectionListener)。 现在我需要之前选择的项目/索引。

到目前为止,我自己保存了最后选择的项目。有更好的方法吗? 换句话说:是否有我多年来怀念的方法/最佳实践?!

I have a JList and register a selection handler (ListSelectionListener).
Now I need to now the previous selected item/index.

Up to now, I save the last selected item on my own. Is there a better way to do so?
In other words: Is there are methode/best practice which I miss all the years?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

贩梦商人 2025-01-10 08:54:25

我的列表之一只能进行单选。就像克利奥帕特拉说的那样。事件数据在这里没有帮助。

克利奥帕特拉不是这么说的。事件数据确实有帮助。您不能假设第一个索引代表所选行,最后一个索引代表前一行。

正如克利奥帕特拉建议的那样,你需要做进一步的检查。像这样的东西:

public void valueChanged(ListSelectionEvent e)
{
    JList list = (JList)e.getSource();
    int selected = list.getSelectedIndex();
    int previous = selected == e.getFirstIndex() ? e.getLastIndex() : e.getFirstIndex();

    System.out.println();
    System.out.println("Selected:" + selected);
    System.out.println("Previous:" + previous);
}

One of my list is single-selection-only. like kleopatra says. The event data does not help here.

That is not what Kleopatra said. The event data does help. You just can't assume that the first index represents the selected row and the last index represents the previous row.

As Kleopatra suggested you need to do further checking. Something like:

public void valueChanged(ListSelectionEvent e)
{
    JList list = (JList)e.getSource();
    int selected = list.getSelectedIndex();
    int previous = selected == e.getFirstIndex() ? e.getLastIndex() : e.getFirstIndex();

    System.out.println();
    System.out.println("Selected:" + selected);
    System.out.println("Previous:" + previous);
}
弥繁 2025-01-10 08:54:25

您无需编写自定义代码即可将先前选定的项目存储在列表中。
JList 提供了一个 ListSelectionListener 它将为您完成这项工作。
这是获取最后选择的项目的方法。

 customList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent arg0) {
             // TODO Auto-generated method stub

                         //Previous Selected Item index will be obtained by arg0.getFirstIndex()
                       // Similarly Currently Selected Item index will be obtained by this method arg0.getLastIndex()



        }
    });

You don't need to write your custom code to store the previous selected item in list.
JList provide a ListSelectionListener which will do the work for you.
Here is the way to get last selected item.

 customList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent arg0) {
             // TODO Auto-generated method stub

                         //Previous Selected Item index will be obtained by arg0.getFirstIndex()
                       // Similarly Currently Selected Item index will be obtained by this method arg0.getLastIndex()



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