我们怎样才能在java摇摆中动态地将2个组合框选定的项目放到第3个组合框中?

时间:2014-09-08 06:52:27

标签: java netbeans jcombobox

这是我在android中所做的,这里我有3个微调器并选择          2个单独的微调器项目,这些项目必须动态显示         第三个旋转器。而我正试图在java摇摆网豆,但我不是         得到,如何制作请帮助我。          在java swings中,我使用的是组合框而不是微调器。

  public void onItemSelected(AdapterView<?> p, View arg1, int arg2, long arg3)     {

 switch (p.getId()) {

         case R.id.spinnerPetition:

                i = spinPetition.getSelectedItem().toString();
            spinnerArray1[0] = i;
            Log.d("R", "" + Arrays.toString(spinnerArray1));

            bind();
            break;

        case R.id.spinnerRespondent:

                j = spinRespondent.getSelectedItem().toString();

                spinnerArray1[1] = j;
                Log.d("R", "" + Arrays.toString(spinnerArray1));

                bind();
                break;

            default:
                break;
    }

    private void bind() {

    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
            android.R.layout.simple_spinner_dropdown_item, spinnerArray1);
    spinAppearingFor.setAdapter(spinnerArrayAdapter);

}

1 个答案:

答案 0 :(得分:0)

基本上,如果第三个字段永远不会影响前两个字段,那么它可能更容易使用JTextField,否则您将需要构建一个ComboBox,这是一个con {前两个,所以它包含每个可能的组合......

Combos

import java.awt.EventQueue;    import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestFields {

    public static void main(String[] args) {
        new TestFields();
    }

    public TestFields() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JComboBox cb1;
        private JComboBox cb2;
        private JTextField field;

        public TestPane() {
            setLayout(new GridBagLayout());

            cb1 = new JComboBox(new Object[]{"Banana", "Pear", "Grapes", "Apples"});
            cb2 = new JComboBox(new Object[]{"Monkey", "Elephant", "Dragon", "Gridalo"});
            field = new JTextField(10);
            field.setEditable(false);

            cb1.setSelectedItem(null);
            cb2.setSelectedItem(null);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(2, 2, 2, 2);
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(cb1, gbc);
            gbc.gridy++;
            add(cb2, gbc);

            gbc.gridx++;
            gbc.gridy = 0;
            gbc.gridheight = GridBagConstraints.REMAINDER;
            add(field, gbc);

            ActionListener handler = new ActionListener() {

                public String getDisplayValue(Object value) {
                    return value == null ? "" : value.toString();
                }

                @Override
                public void actionPerformed(ActionEvent e) {
                    field.setText(
                                    getDisplayValue(cb1.getSelectedItem()) + "/" + 
                                    getDisplayValue(cb2.getSelectedItem()));
                }
            };

            cb1.addActionListener(handler);
            cb2.addActionListener(handler);

        }

    }

}