如何创建动态JList? [英] How can I create dynamic JLists?

查看:53
本文介绍了如何创建动态JList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个Jlist组件.选择第一个列表中的项目时,下一个列表应动态显示一组新的可能选择.

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.

例如,第一个列表可能包含三个项目,分别是"A","B","C".当我单击"A"时,下一个列表应显示1、2、3、4、5等.当我单击"B"时,下一个列表应显示7、8、9等.这种逻辑.

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.

目标是实现这样的GUI:

The aim is to implement a GUI like this:

推荐答案

概述

在选择处理程序中,使用setModel()将第二个列表的模型设置为当前选择的正确ListModel.

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()));
    }
});

  • 类似地,在第二个JList中添加ListSelectionListener并相应地更新第三个面板.

  • Similarly, add a ListSelectionListener to the second JList and update the third panel accordingly.

    对于ComboBoxModel此处显示了类似的方法.此相关的示例使用类似的方法在列中显示文件系统树.

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

    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);
        }
    }
    

    这篇关于如何创建动态JList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆