1

I have multiple Jlist components. When an item in the first list is selected, the next list should dynamically display a new set of possible selections.

For example, the first list might have three items which are "A" ,"B" ,"C". When I click "A", the next list should show 1, 2, 3, 4, 5, etc. When I click "B" the next list should show 7, 8, 9, etc. I need these lists to work on this logic.

general list scheme

The aim is to implement a GUI like this:

specific GUI

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
sergen boğa
  • 35
  • 1
  • 11
  • On selection, update the `ListModel` of the dependent list, as shown [here](http://stackoverflow.com/a/3191882/230513) for `ComboBoxModel`. – trashgod Jan 06 '17 at 21:24
  • 1
    Except for the list selection behavior implied by the labels above them, it would seen that a `JTree` is better to display the items. But trying to fit the selection suggestion of list B (Single Interval) to your original description, what happens if the user selects A in the first list, then 2, 3, 4 in the 2nd? What appears in the 3rd list in that event? BTW - I find it easier to give advice when the problem is more 'real' and less 'abstract'. To help with that, what are the items in the 3 lists? – Andrew Thompson Jan 06 '17 at 21:28
  • http://i.hizliresim.com/MoRJ2Q.png I try to implement some thing like this. It doesn't has to have 3th list it can be panel for information. – sergen boğa Jan 06 '17 at 22:05
  • Tip: Add @trashgod (or whoever, the `@` is important) to *notify* a person of a new comment. Now, looking at the last image (that I've now added to the question) it seems that one team from one league will be selected at any time, with the Info panel showing the details of that team. (Correct me if I got that wrong.) I would still suggest using a `JTree` - the leagues would be top level nodes, and the teams would be leaves of those nodes. Selecting any team would result in the team's details to be shown in the Info panel. I feel this would suit the user better than having two lists. – Andrew Thompson Jan 06 '17 at 23:04
  • @AndrewThompson, this screenshoot belongs our low fidelity of UI. I thought about using jtree but I prefer to use List. I decided to do that for appearance and functionality. I don't want to change my draft at this stage of the project. – sergen boğa Jan 06 '17 at 23:22
  • @AndrewThompson: Agree about `JTree`; an alternate view using `JList` is seen [here](http://stackoverflow.com/a/15104660/230513). – trashgod Jan 06 '17 at 23:43

1 Answers1

4

In outline,

  • Add a ListSelectionListener to the first JList.

  • In the selection handler, use setModel() to set the second list's model to the correct ListModel for the current selection.

    list1.addListSelectionListener((ListSelectionEvent e) -> {
        if (!e.getValueIsAdjusting()) {
            list2.setModel(models.get(list1.getSelectedIndex()));
        }
    });
    
  • Similarly, add a ListSelectionListener to the second JList and update the third panel accordingly.

A similar approach is shown here for ComboBoxModel. This related example uses a similar approach to display a file system tree in columns.

select A select B

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;

/** @see https://stackoverflow.com/a/41519646/230513 */
public class DynamicJList {

    private final JList<String> list1 = new JList<>(new String[]{"A", "B"});
    private final JList<String> list2 = new JList<>();
    private final List<DefaultListModel> models = new ArrayList<>();

    private void display() {
        JFrame f = new JFrame("DynamicJList");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DefaultListModel<String> model1 = new DefaultListModel<>();
        model1.addElement("A1");
        model1.addElement("A2");
        model1.addElement("A3");
        models.add(model1);
        DefaultListModel<String> model2 = new DefaultListModel<>();
        model2.addElement("B1");
        model2.addElement("B2");
        models.add(model2);
        list2.setModel(model1);
        list1.addListSelectionListener((ListSelectionEvent e) -> {
            if (!e.getValueIsAdjusting()) {
                list2.setModel(models.get(list1.getSelectedIndex()));
            }
        });
        JPanel panel = new JPanel(new GridLayout(1, 0));
        panel.add(list1);
        panel.add(list2);
        f.add(panel);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new DynamicJList()::display);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045