使用数据库刷新jcombo box java中的内容

使用数据库刷新jcombo box java中的内容,java,mysql,database,swing,jcombobox,Java,Mysql,Database,Swing,Jcombobox,在第一个组合框中选择项目后,是否可以在一个组合框中刷新内容。第一个组合框中的项目是类型,第二个组合框中的项目是子类型。此外,这必须发生在一个帧上。组合框中的数据将从数据库中读取。类型是数据库中的一列,子类型也是。我使用的数据库是MYSQL 对快速简单代码有什么建议吗 我有一个很好的图像来说明这个问题,但我需要更多的代表来添加它 谢谢 用于存储用于存储的项目 从RunnableThread或SwingWorker调用数据库事件,将这个可能很难执行且长时间运行的任务重定向到Workers线程,否则S

在第一个组合框中选择项目后,是否可以在一个组合框中刷新内容。第一个组合框中的项目是类型,第二个组合框中的项目是子类型。此外,这必须发生在一个帧上。组合框中的数据将从数据库中读取。类型是数据库中的一列,子类型也是。我使用的数据库是MYSQL

对快速简单代码有什么建议吗

我有一个很好的图像来说明这个问题,但我需要更多的代表来添加它

谢谢

用于存储用于存储的项目

从RunnableThread或SwingWorker调用数据库事件,将这个可能很难执行且长时间运行的任务重定向到Workers线程,否则Swing GUi将冻结或对鼠标和键事件无响应,直到JDBC结束

很好地保证了所有输出都将在EDT上完成

比如说

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);
        //prevent action events from being fired when the up/down arrow keys are used
        //mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
        getContentPane().add(mainComboBox, BorderLayout.WEST);
        subComboBox = new JComboBox();//  Create sub combo box with multiple models
        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);
//      mainComboBox.setSelectedIndex(1);
    }

    @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 { //sipmle simulation of JDBC events, by using Swing Timer

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

这太棒了!!谢谢,但我想知道的一个主要部分是,我如何从从数据库读取的第一个组合框中获取数据,并从第二个组合框中读取数据,第二个组合框仅适用于在第一个组合框中选择的答案?因此,您可以将数据库中的数据放在该数组中,而不是填充一个数组。