简体   繁体   English

MySQL Java使用JComboBox设置第二个JComboBox

[英]MySQL Java Use a JComboBox to set a second JComboBox

I am using MySQL server 5.6 to host a database that I am using to manage part number records. 我正在使用MySQL服务器5.6托管用于管理部件号记录的数据库。 I have a Java client front end. 我有一个Java客户端前端。 My connections between the client and database are all good. 我在客户端和数据库之间的连接都很好。 In my panel to create parts, I have a JComboBox-typeComboBox that is populated by querying the database for all possible part types and displaying their type numbers in the drop down menu. 在用于创建零件的面板中,我有一个JComboBox-typeComboBox,它是通过查询数据库中所有可能的零件类型并在下拉菜单中显示其类型号来填充的。 Depending on the selection from that JComboBox the tdescripTextField and seqTextField are filled with the the appropriate information but I can not get the JComboBox-matComboBox to be populated by the appropriate results that are queried using the selected PartType number. 根据从该JComboBox进行的选择,tdescripTextField和seqTextField填充有适当的信息,但是我无法获得JComboBox-matComboBox来填充使用选定的PartType编号查询的适当结果。

I have used a number of System.out.println(); 我已经使用了许多System.out.println(); to debug the part of the code where the issue is happening, and from what I can tell the String[] mats does contain the correct information, but it just wont assign it to the matComboBox. 调试发生问题的代码部分,据我所知,String []席位确实包含正确的信息,但不会将其分配给matComboBox。 I notifced if I don't initialize matComboBox = new JComboBox(); 如果未初始化matComboBox = new JComboBox(),则会通知我; right after the typeComboBox I get a compilation error saying that matComboBox is null. 在typeComboBox之后,我得到一个编译错误,说matComboBox为null。

Thanks in advance for any help. 在此先感谢您的帮助。

ps all of my query methods return JSONArrays from the database. ps我所有的查询方法都从数据库返回JSONArrays。

class CreatePanel extends JPanel{
    //JButtons  
        private JButton saveButton;
        private JButton backButton;

    //JComboBox
        private JComboBox<?> typeComboBox;
        private JComboBox<?> matComboBox;

    //JTextField
        private JTextField tdescripTextField;
        private JTextField mdescripTextField;
        private JTextField descripTextField;
        private JTextField seqTextField;
        private JTextField bpartTextField;
        private JTextField cpartTextField;
        private JTextField spartTextField;

    //JLabel
        private JLabel lblSeq;
        private JLabel lblDescription;
        private JLabel lblMatterialDescription;
        private JLabel lblTypeDescription;
        private JLabel lblType;
        private JLabel lblMatterial;
        private JLabel lblBosalPartNumber;
        private JLabel lblCustomerPartNumber;
        private JLabel lblSupplierPartNumber;
        private JLabel lblCreateAPart;
        private JLabel Bosal;
        JPanel contentPane;

    //StringPanel   
        public CreatePanel(final JPanel create)
        {
    //TextFields

            tdescripTextField = new JTextField();
            tdescripTextField.setEditable(false);
            mdescripTextField = new JTextField();
            mdescripTextField.setEditable(false);
            descripTextField = new JTextField();
            seqTextField = new JTextField();
            seqTextField.setEditable(false);
            bpartTextField = new JTextField();
            bpartTextField.setEditable(false);
            cpartTextField = new JTextField();
            spartTextField = new JTextField();

    //ComboBoxes
            typeComboBox = new JComboBox<Object>();
            matComboBox = new JComboBox<Object>();
            JSONArray temp1 = new JSONArray();

            String[] types = null;

            try{
                temp1 = con.queryReturnAllTypes();
                types = new String[temp1.length()];

                for(int i = 0; i < temp1.length(); i++){
                    types[i] = temp1.getJSONObject(i).get("PartType").toString();
                }
            }catch(Exception ex){/*ignore*/}

            typeComboBox = new JComboBox<Object>(types);
            typeComboBox.setEditable(true);
            typeComboBox.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e){
                    int partType = Integer.valueOf((String) typeComboBox.getSelectedItem());
                    /*Debug*/System.out.println(partType);
                    JSONArray temp1 = new JSONArray();
                    JSONArray temp2 = new JSONArray();
                    String typeDescrip = null;
                    String seqNum = null;
                    String[] mats = null;

                    try{
                        temp1 = con.queryPartType(partType);
                        /*Debug*/System.out.println(temp1);
                        temp2 = con.queryMaterialPartType(partType);
                        /*Debug*/System.out.println(temp2);
                        mats = new String[temp2.length()];
                        /*Debug*/System.out.println(temp2.length());

                        typeDescrip = temp1.getJSONObject(0).get("TypeDescription").toString();
                        tdescripTextField.setText(typeDescrip);
                        seqNum = temp1.getJSONObject(0).get("SeqNumber").toString();
                        seqTextField.setText(seqNum);

                        for(int i = 0; i < temp2.length(); i++){
                            /*Debug*/System.out.println(i);
                            /*Debug*/System.out.println(temp2.getJSONObject(i).get("Material").toString());
                            mats[i] = temp2.getJSONObject(i).get("Material").toString();
                            /*Debug*/System.out.println(mats[i]);
                        }
                        for(int i = 0; i<mats.length; i++){
                            /*Debug*/System.out.println(mats[i]);

                        }
                    }catch(Exception ex){/*ignore*/}    
                        for(int i = 0; i<mats.length; i++){
                            /*Debug*/System.out.println(mats[i]);

                        }
                    matComboBox = new JComboBox<Object>(mats);
                    matComboBox.setEditable(true);
                }

            });

    //Labels

            lblType = new JLabel("Type");
            lblMatterial = new JLabel("Material");
            lblTypeDescription = new JLabel("Type Description");
            lblMatterialDescription = new JLabel("Material Description");
            lblSeq = new JLabel("Seq");
            lblDescription = new JLabel("Description");
            lblBosalPartNumber = new JLabel("Bosal Part Number");
            lblCustomerPartNumber = new JLabel("Customer Part Number");
            lblSupplierPartNumber = new JLabel("Supplier Part Number");
            lblCreateAPart = new JLabel("Create a Part Number");

            ImageIcon bosal = new ImageIcon(getClass().getResource("/Images/bosal.jpg"));
            Bosal = new JLabel(bosal);
            setBackground(Color.DARK_GRAY);

    //Buttons

            ImageIcon save = new ImageIcon(getClass().getResource("/images/save1.jpg"));
            saveButton = new JButton(save);
            saveButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == saveButton){
                        int n = JOptionPane.showConfirmDialog(
                                frame,
                                "Are you sure you want to save part data?",
                                "Save:",
                                JOptionPane.YES_NO_OPTION,
                                JOptionPane.WARNING_MESSAGE);

                    }}});
            add(saveButton);

            ImageIcon back = new ImageIcon(getClass().getResource("/images/back1.jpg"));
            backButton = new JButton(back);
            backButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == backButton)
                    {

                        setVisible(false);
                        frame.setLocation(550,220);
                        frame.setSize(700, 580);
                        main.setVisible(true);

                    }}});
            add(backButton);

            setupPanel();

        };

        private void setupPanel() 

        {

    //Label Fonts

            lblType.setFont(new Font("Tahoma", Font.BOLD, 14));
            lblType.setForeground(Color.BLACK);
            lblMatterial.setFont(new Font("Tahoma", Font.BOLD, 14));
            lblMatterial.setForeground(Color.BLACK);
            lblTypeDescription.setFont(new Font("Tahoma", Font.BOLD, 14));
            lblTypeDescription.setForeground(Color.BLACK);
            lblMatterialDescription.setFont(new Font("Tahoma", Font.BOLD, 14));
            lblMatterialDescription.setForeground(Color.BLACK);
            lblDescription.setFont(new Font("Tahoma", Font.BOLD, 14));
            lblDescription.setForeground(Color.BLACK);
            lblSeq.setFont(new Font("Tahoma", Font.BOLD, 14));
            lblSeq.setForeground(Color.BLACK);
            lblSupplierPartNumber.setFont(new Font("Tahoma", Font.BOLD, 14));
            lblSupplierPartNumber.setForeground(Color.BLACK);
            lblCreateAPart.setFont(new Font("EucrosiaUPC", Font.BOLD, 64));
            lblCreateAPart.setForeground(Color.BLACK);
            lblCustomerPartNumber.setFont(new Font("Tahoma", Font.BOLD, 14));
            lblCustomerPartNumber.setForeground(Color.BLACK);
            lblBosalPartNumber.setFont(new Font("Tahoma", Font.BOLD, 14));
            lblBosalPartNumber.setForeground(Color.BLACK);

    //Group Layout  

            GroupLayout groupLayout = new GroupLayout(this);
            groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                    .addGroup(groupLayout.createSequentialGroup()
                        .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                            .addGroup(groupLayout.createSequentialGroup()
                                .addGap(24)
                                .addComponent(Bosal, GroupLayout.PREFERRED_SIZE, 199, GroupLayout.PREFERRED_SIZE)
                                .addGap(6)
                                .addComponent(lblCreateAPart, GroupLayout.PREFERRED_SIZE, 434, GroupLayout.PREFERRED_SIZE))
                            .addGroup(groupLayout.createSequentialGroup()
                                .addGap(50)
                                .addComponent(lblType)
                                .addGap(121)
                                .addComponent(lblTypeDescription)
                                .addGap(163)
                                .addComponent(lblBosalPartNumber))
                            .addGroup(groupLayout.createSequentialGroup()
                                .addGap(50)
                                .addComponent(typeComboBox, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
                                .addGap(76)
                                .addComponent(tdescripTextField, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
                                .addGap(67)
                                .addComponent(bpartTextField, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
                            .addGroup(groupLayout.createSequentialGroup()
                                .addGap(50)
                                .addComponent(lblMatterial)
                                .addGap(101)
                                .addComponent(lblMatterialDescription)
                                .addGap(143)
                                .addComponent(lblCustomerPartNumber))
                            .addGroup(groupLayout.createSequentialGroup()
                                .addGap(50)
                                .addComponent(matComboBox, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
                                .addGap(76)
                                .addComponent(mdescripTextField, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
                                .addGap(67)
                                .addComponent(cpartTextField, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
                            .addGroup(groupLayout.createSequentialGroup()
                                .addGap(50)
                                .addComponent(lblSeq)
                                .addGap(129)
                                .addComponent(lblDescription)
                                .addGap(201)
                                .addComponent(lblSupplierPartNumber))
                            .addGroup(groupLayout.createSequentialGroup()
                                .addGap(331)
                                .addComponent(backButton, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE)
                                .addGap(41)
                                .addComponent(saveButton, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
                            .addGroup(groupLayout.createSequentialGroup()
                                .addGap(50)
                                .addComponent(seqTextField, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                                .addGap(97)
                                .addComponent(descripTextField, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
                                .addGap(67)
                                .addComponent(spartTextField, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(36, Short.MAX_VALUE))
            );
            groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                    .addGroup(groupLayout.createSequentialGroup()
                        .addGap(32)
                        .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                            .addComponent(Bosal, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblCreateAPart, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE))
                        .addGap(6)
                        .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                            .addComponent(lblType)
                            .addComponent(lblTypeDescription)
                            .addComponent(lblBosalPartNumber))
                        .addGap(6)
                        .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                            .addComponent(typeComboBox, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
                            .addGroup(groupLayout.createSequentialGroup()
                                .addGap(3)
                                .addComponent(tdescripTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addGroup(groupLayout.createSequentialGroup()
                                .addGap(3)
                                .addComponent(bpartTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                        .addGap(6)
                        .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                            .addComponent(lblMatterial)
                            .addComponent(lblMatterialDescription)
                            .addComponent(lblCustomerPartNumber))
                        .addGap(6)
                        .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                            .addComponent(matComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(mdescripTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(cpartTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGap(9)
                        .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                            .addComponent(lblSeq)
                            .addComponent(lblDescription)
                            .addComponent(lblSupplierPartNumber))
                        .addGap(6)
                        .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                            .addComponent(seqTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(descripTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(spartTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGap(37)
                        .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                            .addComponent(saveButton, 0, 0, Short.MAX_VALUE)
                            .addComponent(backButton, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(135, Short.MAX_VALUE))
            );
            setLayout(groupLayout); 
}}

You already have a combo box, which is already displayed in the panel. 您已经有一个组合框,该组合框已显示在面板中。 What you want is to popule this already existing combo box. 您要填充此现有组合框。

But your code does 但是你的代码确实

matComboBox = new JComboBox<Object>(mats);

So it creates another JComboBox, populated with the mats that you retrieved. 因此,它将创建另一个JComboBox,其中填充了您检索到的mats That won't popukate your existing, displayed combo box. 那不会弹出您现有的显示的组合框。 What you need to do is to change the data inside the combo box: 您需要做的是更改组合框中的数据:

matComboBox.setModel(new DefaultComboBoxModel(mats));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM