如何从第二个SubComboBox获取值?

时间:2015-03-04 15:52:57

标签: java jcombobox

我是新来的,我需要有关Java代码的帮助。在这里提问是我的最后一个选择,你去了。我已经挤掉了脑中的最后一点。我没有得到我想要的输出。此代码发布在stackoverflow中。这段代码与我想创建的内容完全相同。

我只想获得第二个JComboBox的值。

例如:

我点击了#34; Shape"在第一个ComboBox中。然后我选择" Triangle"。然后最后一部分是我会放一些或者它会显示关于那个" Triangle"的描述。喜欢" Triangle有三面。"类似的东西。

另一个例子

我选择" Color"在第一个ComboBox中。然后我点击了" Red"。然后我想要显示颜色的描述" Red"在输出的最后部分。

在JComboBox中选择后的输出示例是:

"形状:三角形

Triangle有三面。"

我希望这是有道理的。

这是示例代码..

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;

  public class ComboBoxTwo extends JFrame implements ActionListener,   ItemListener {

private static final long serialVersionUID = 1L;
private JComboBox mainComboBox;
private JComboBox subComboBox;
private Hashtable<Object, Object> subItems = new Hashtable<Object, Object> ();

public ComboBoxTwo() {
    String[] items = {"Select Item", "Color", "Shape", "Fruit"};
    mainComboBox = new JComboBox(items);
    mainComboBox.addActionListener(this);
    mainComboBox.addItemListener(this);

    getContentPane().add(mainComboBox, BorderLayout.WEST);
    subComboBox = new JComboBox();

    subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
    subComboBox.addItemListener(this);
    getContentPane().add(subComboBox, BorderLayout.EAST);
    String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
    subItems.put(items[1], subItems1);
    String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
    subItems.put(items[2], subItems2);
    String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
    subItems.put(items[3], subItems3);

}

@Override
public void actionPerformed(ActionEvent e) {
    String item = (String) mainComboBox.getSelectedItem();
    Object o = subItems.get(item);
    if (o == null) {
        subComboBox.setModel(new DefaultComboBoxModel());
    } else {
        subComboBox.setModel(new DefaultComboBoxModel((String[]) o));
    }
}

@Override
public void itemStateChanged(ItemEvent e) {
    if (e.getStateChange() == ItemEvent.SELECTED) {
        if (e.getSource() == mainComboBox) {
            if (mainComboBox.getSelectedIndex() != 0) {
                FirstDialog firstDialog = new FirstDialog(ComboBoxTwo.this,
                        mainComboBox.getSelectedItem().toString(), "Please wait,  Searching for ..... ");
            }
        } 
    }
}

private class FirstDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    FirstDialog(final Frame parent, String winTitle, String msgString) {
        super(parent, winTitle);
        setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        JLabel myLabel = new JLabel(msgString);
        JButton bNext = new JButton("Stop Processes");
        add(myLabel, BorderLayout.CENTER);
        add(bNext, BorderLayout.SOUTH);
        bNext.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                setVisible(false);
            }
        });
        javax.swing.Timer t = new javax.swing.Timer(1000, new      
ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
        t.setRepeats(false);
        t.start();
        setLocationRelativeTo(parent);
        setSize(new Dimension(400, 100));
        setVisible(true);
    }
}

public static void main(String[] args) {
    JFrame frame = new ComboBoxTwo();
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
} ///////

1 个答案:

答案 0 :(得分:0)

这有效

package test1;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

public class ComboBoxTwo extends JFrame implements ActionListener, ItemListener {

    private static final long serialVersionUID = 1L;
    private JComboBox<String> mainComboBox;
    private JComboBox<String> subComboBox;

    private JLabel label;
    private String[][] descriptions;

    private Hashtable<Object, Object> subItems = new Hashtable<Object, Object> ();

    public ComboBoxTwo() {
        String[] items = {"Select Item", "Color", "Shape", "Fruit"};
        this.getContentPane().setLayout(null);

        this.setSize(400, 400);

        mainComboBox = new JComboBox<String>(items);
        mainComboBox.addActionListener(this);
        mainComboBox.addItemListener(this);
        mainComboBox.setSize(100, 100);
        mainComboBox.setLocation(0, 100);


        subComboBox = new JComboBox<String>();  
        subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
        subComboBox.addItemListener(this);
        subComboBox.setSize(100, 100);
        subComboBox.setLocation(this.getSize().width - 120, 100);


        String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
        subItems.put(items[1], subItems1);

        String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
        subItems.put(items[2], subItems2);

        String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
        subItems.put(items[3], subItems3);


        label = new JLabel();
        label.setSize(300, 25);
        label.setLocation(this.getSize().width /2 - label.getSize().width /2, 25);
        label.setText("This is a label: currently nothing selected");
        label.setVisible(true);


        getContentPane().add(subComboBox);
        getContentPane().add(mainComboBox);
        getContentPane().add(label);

        descriptions = new String[][]
        { 
            {"The red color", "The blue color", "A green color"}, 
            {"This shape is round", "Looks rectangular", "Has three sides"}, 
            {"Didn't fall far from the tree", "Seems quite orangy", "Look i've got a banana in my trousers"} 
        };    
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String item = (String) mainComboBox.getSelectedItem();
        Object o = subItems.get(item);
        if (o == null) {
            subComboBox.setModel(new DefaultComboBoxModel<String>());
        } else {
            subComboBox.setModel(new DefaultComboBoxModel<String>((String[]) o));
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {

            if (mainComboBox.getSelectedIndex() == 0 && subComboBox.getSelectedIndex() == 0)
            {
                label.setText("This is a label: currently nothing selected");
            }
            else if (mainComboBox.getSelectedIndex() >= 0)
            {
                label.setText("The first combobox has been altered");
            }

            if (e.getSource() == mainComboBox) {
                if (mainComboBox.getSelectedIndex() != 0) {
                    FirstDialog firstDialog = new FirstDialog(ComboBoxTwo.this,
                            mainComboBox.getSelectedItem().toString(), "Please wait,  Searching for ..... ");
                }
            }
            else if (e.getSource() == subComboBox)
            {
                label.setText(descriptions[mainComboBox.getSelectedIndex() -1][((JComboBox<String>) e.getSource()).getSelectedIndex() -1]);
            }
        }
    }

    private class FirstDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        FirstDialog(final Frame parent, String winTitle, String msgString) {
            super(parent, winTitle);
            setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
            JLabel myLabel = new JLabel(msgString);
            JButton bNext = new JButton("Stop Processes");
            add(myLabel, BorderLayout.CENTER);
            add(bNext, BorderLayout.SOUTH);
            bNext.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    setVisible(false);
                }
            });
            javax.swing.Timer t = new javax.swing.Timer(1000, new  ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                }
            });
            t.setRepeats(false);
            t.start();
            setLocationRelativeTo(parent);
            setSize(new Dimension(400, 100));
            setVisible(true);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new ComboBoxTwo();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}