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.
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);
}
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With