【问题标题】:What is the "best" way to fluidly reorder a JList?流畅地重新排序 JList 的“最佳”方法是什么?
【发布时间】:2012-04-20 07:32:22
【问题描述】:

我正在制作一个基于摇摆的应用程序,其中我有一个 JList,它会定期使用相同数据的不同顺序进行更新,很少使用新项目进行更新,也很少使用较少的项目进行更新。我正在尝试找出使这看起来不错的最佳方法。现在,我只是打电话给

JList.setListData(String [] data);

这看起来不太好,它会清除选定的项目。

我想要一种更新它的方法,如果它从列表中删除,则只清除选定的项目,否则保持相同的项目被选中,即使它们的索引发生变化。我正在研究跟踪选择了哪些索引,然后在更改数据后设置选定的项目,但这听起来很可怕。我还查看了 ListModels 并跟踪我自己的,但似乎选择的项目保存在 JList 本身中,因此也不能完美地工作。

非常感谢任何建议。

【问题讨论】:

  • setListData()的实现是否相关? sscce,例如 this one 可能会有所帮助。
  • 使用 JXList(Swingx 的一部分),它支持 ui 级别的排序/过滤。请注意,在批量修改通知期间所有模型装饰解决方案都无效(通常我们可以逃脱,因为更常见的 chasnge 粒度是单项:-)

标签: java swing list jlist


【解决方案1】:

我给你做了一个有效的例子。

我已经按照您的要求使用了JList,添加了从列表中选择、添加和删除的按钮。

无论如何,希望这会有所帮助!

干杯。

public class Test {

    private static String selectedValue = null;
    private static JList jList = new JList();

    public static void main(String args[]) {
        JFrame jFrame = new JFrame();
        jFrame.setSize(500, 500);
        jFrame.setLocationRelativeTo(null);
        jFrame.setLayout(new GridLayout(4, 1));

        jList = new JList(new String[]{"1", "2", "3", "4", "5"});
        jFrame.add(jList);

        JButton rearrangeButton = new JButton("rearrange");
        rearrangeButton.addActionListener(new Test().new SelectedListener());
        jFrame.add(rearrangeButton);

        JButton addButton = new JButton("add");
        addButton.addActionListener(new Test().new SelectedListener());
        jFrame.add(addButton);

        JButton deleteButton = new JButton("delete");
        deleteButton.addActionListener(new Test().new SelectedListener());
        jFrame.add(deleteButton);

        jFrame.setVisible(true);
    }

    private class SelectedListener implements ActionListener {

        public void actionPerformed(ActionEvent actionEvent) {
            storeSelected();

            if (actionEvent.getActionCommand().equalsIgnoreCase("rearrange")) {
                jList.setListData(new String[]{"5", "4", "3", "2", "1"});
            } else if (actionEvent.getActionCommand().equalsIgnoreCase("add")) {
                List< String> items = new ArrayList< String>();

                for (int item = 0; item < jList.getModel().getSize(); item++) {
                    items.add(jList.getModel().getElementAt(item).toString());
                }

                items.add("new");

                jList.setListData(items.toArray());
            } else if (actionEvent.getActionCommand().equalsIgnoreCase("delete")) {
                List< String> items = new ArrayList< String>();

                for (int item = 0; item < jList.getModel().getSize(); item++) {
                    items.add(jList.getModel().getElementAt(item).toString());
                }

                items.remove(0);

                jList.setListData(items.toArray());
            }

            reSelect();
        }
    }

    private static void storeSelected() {
        if (jList.getSelectedIndex() > -1) {
            selectedValue = jList.getSelectedValue().toString();
        } else {
            selectedValue = null;
        }
    }

    private static void reSelect() {
        if (selectedValue != null) {
            jList.setSelectedValue(selectedValue, true);
        }
    }
}

【讨论】:

  • +1 表示sscce。您将如何处理多项选择?编辑:对不起大括号。 :-)
  • 这看起来很棒,我会研究一下并试一试
  • 我通过编辑 storeSelected() 来处理多个选择以保存值数组 (JList.getSelectedValues()),然后在重新选择时,我计算每个值的索引,然后使用 setSelectedIndices (int [] indices) 来设置它们。这不是最干净的,但它有效。感谢您的帮助!!!
【解决方案2】:

似乎标准JList 没有调整排序的方法(为什么他们只将其添加到JTable ...),但有一个tutorial for sorted lists

【讨论】:

  • 这是一篇好文章,看起来它是正确的做法。
  • 与模型装饰方法一样的问题:在通知批量更改期间,排序模型状态未定义...
  • @kleopatra 有什么好的解决方案吗?我看到 SwingX 项目的 JXList 提供了视图排序,但我不熟悉它背后的源代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 2013-12-29
  • 1970-01-01
  • 2021-06-09
相关资源
最近更新 更多