English 中文(简体)
A. 动态分析Box和ComboBox
原标题:Dynamic CheckBox and ComboBox

I have at problem with dynamic checkboxes. I want a combobox to show beside the checbox/checkboxes when one or more is selected. But I only get one combobox, which moves around whenever I select a new checkbox. So can anybody helpe me by telling me whats wrong and how I get more than one combobox?

for(int i = 0; i < names.length; i++) {
        // ParameterField
        JTextField fieldParam = new JTextField();
        fieldParam.setText(names[i]);
        fieldParam.setEditable(false);
        addElement(contentPanel, fieldParam, 25, yPos, 100, 20);

        // ValueField
        JTextField fieldValue = new JTextField();
        fieldValue.setText("" + values[i]);
        fieldValue.setEditable(false);
        addElement(contentPanel, fieldValue, 160, yPos, 100, 20);

        //RadioButtonField          
        final JCheckBox checkboxes = new JCheckBox();
        checkboxes.setActionCommand(names[i]);
        checkboxes.addActionListener(this);
        addElement(contentPanel, checkboxes, 325, yPos, 100, 20);


        final int checkBoxIndex = i;
        checkboxes.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae2) {
                if (checkboxes.isSelected()) {
                    comboProcent.setEnabled(true);
                    comboProcent.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"2%", "5%", "10%"}));
                    addElement(contentPanel, comboProcent, 435, 50 + checkBoxIndex * 25, 80, 20);

                    setVisible(true);

                }

                    if (!checkboxes.isSelected()) {
                    contentPanel.remove(comboProcent);

                }

                System.out.println("checkbox is: " + checkBoxIndex);
            }
        });

        yPos = yPos + 25;
    }
问题回答

You need to create a new combo box instance for each checkbox. The best way I think would be to change their visibility based on when the checkboxes values have changed. (I ve done it this way before)

i 专注知道,如果你想要一个二分点子,那么我是否正确理解了,但如果你想要使用自己的ComboBoxModel。





相关问题
WPF Datagrid, Setting the background of combox popup

I would like to change the color of the popup background when using a DatagridComboboxColumn in the WPF Toolkit datagrid. I ve edited the Template for a normal Combobox and it works great for selected ...

How to insert ComboBox item into ListBox? [winforms]

The question is very simple, How to insert ComboBox selected item into ListBox using c#? I have tried with this: listbox.Items.Add(combobox.SelectedItem); and some other permutations but it always ...

How do I bind a ComboBox to a one column list

I ve seen how to bind a ComboBox to a list that has columns like this: ItemsSource="{Binding Path=Entries}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path=Entry}" But ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

热门标签