根据JList选择动态添加单选按钮

时间:2018-05-19 14:32:41

标签: java swing jlist

我想根据用户在JList中选择的类别动态添加一组单选按钮。这些依次根据选择创建不同的文本框,如下所示。我不确定要在ListSelectionListener匿名内部类中包含多少内容,以及如何进行操作。

从所选类别列表中,为每个类别:

  • 带有3个选项的单选按钮
  • 如果选择了选项1,则输入3个文本框
  • 如果选择了选项2,则输入1个文本框以输入数据
  • 如果选择了选项3,则从列表中删除类别

目前这就是我所拥有的。我在这行代码中也遇到错误:arrayRadioButtonSubcategoriesType.get(1).addActionListener(this);关于传入这个'。我不确定我应该传递的是什么:

MyClass implements ActionListener{

ArrayList<ButtonGroup> arrayButtonGroupsCategories =  new ArrayList<>();
ArrayList<JRadioButton> arrayRadioButtonSubcategoriesType = new ArrayList<>();

MyClass(final Container pane){
    preprocessPanel = new JPanel(new GridLayout(4,2,10,10));
    //other stuff
    categorySubPanel = new JPanel();
    preprocessPanel.add(categorySubPanel);
    preprocessPanel.setVisible(true);
    pane.add(preprocessPanel);
}

private void createCategoryInfoGUI() {
openFile();
categories = new JList<>(getColumnNames());
categories.setVisibleRowCount(5);     
categories.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
preprocessPanel.add(new JScrollPane(categories));
categories.addListSelectionListener(new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent e) {
        if(!e.getValueIsAdjusting()) {
            final List<String> selectedValuesList = categories.getSelectedValuesList();
            System.out.println(selectedValuesList);
        }
        int i = 0;
        for (String key: selectedValuesList){
            arrayRadioButtonSubcategoriesType.add(new JRadioButton("Nominal"));
            arrayRadioButtonSubcategoriesType.get(1).addActionListener(this);
            arrayRadioButtonSubcategoriesType.add(new JRadioButton("Ordinal"));
            arrayRadioButtonSubcategoriesType.add(new JRadioButton("Neither"));
            arrayButtonGroupsCategories.add(new ButtonGroup());
            arrayButtonGroupsCategories.get(i).add(arrayRadioButtonSubcategoriesType.get(i));
            arrayButtonGroupsCategories.get(i+1).add(arrayRadioButtonSubcategoriesType.get(i+1));
            arrayButtonGroupsCategories.get(i+2).add(arrayRadioButtonSubcategoriesType.get(i+2));
            i+=3;
        }
    }
    });
  }
}

0 个答案:

没有答案