Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "best" way to fluidly reorder a JList?

I'm making a swing based application where I have a JList that periodically get's updated with different orderings of the same data, gets infrequently updated with new items, and also infrequently updated with less items. I'm trying to figure out the best way to make this look good. Right now, I just call

JList.setListData(String [] data);

Which doesn't look too great, and it clears the selected items.

I want a way to update it that only clears the selected items if it was removed from the list, but otherwise keeps the same items selected, even if their index changes. I was looking into keeping track of which indexs are selected and then setting the selected items after changing the data but that sounds horrible. I also looked at ListModels and keeping track of my own but it seems that which items are selected is kept in the JList itself so that wouldn't work perfectly either.

I'd really appreciate any suggestions.

like image 594
Nico Avatar asked Feb 21 '23 21:02

Nico


2 Answers

I made you a working example.

I've used the JList like you requested, added buttons that resort, add, and delete from the list.

Anyways, hope this helps!

Cheers.

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);
        }
    }
}
like image 158
Jonathan Payne Avatar answered Mar 04 '23 02:03

Jonathan Payne


It seems the standard JList has no methods to adjust the sorting (why on earth did they only added that to a JTable ...), but there is a tutorial for sorted lists

like image 21
Robin Avatar answered Mar 04 '23 03:03

Robin