0

部品番号レコードの管理に使用しているデータベースをホストするために、MySQL サーバー 5.6 を使用しています。Java クライアント フロント エンドがあります。クライアントとデータベース間の接続はすべて良好です。パーツを作成するためのパネルには、JComboBox-typeComboBox があります。これは、可能なすべてのパーツ タイプをデータベースに照会し、ドロップダウン メニューにタイプ番号を表示することによって取り込まれます。その JComboBox からの選択に応じて、tdescripTextField と seqTextField に適切な情報が入力されますが、選択した PartType 番号を使用してクエリされた適切な結果を JComboBox-matComboBox に取り込むことができません。

多くの System.out.println(); を使用しました。問題が発生しているコードの部分をデバッグし、String[] マットには正しい情報が含まれていることがわかりますが、matComboBox に割り当てられません。matComboBox = new JComboBox(); を初期化しないと通知されました。typeComboBox の直後に、matComboBox が null であるというコンパイル エラーが表示されます。

助けてくれてありがとう。

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

1 に答える 1

1

すでにパネルに表示されているコンボ ボックスがあります。必要なのは、この既存のコンボ ボックスに入力することです。

しかし、あなたのコードは

matComboBox = new JComboBox<Object>(mats);

matsしたがって、取得した が取り込まれた別の JComboBox が作成されます。それはあなたの既存の表示されたコンボボックスをポピュレートしません。必要なことは、コンボ ボックス内のデータを変更することです。

matComboBox.setModel(new DefaultComboBoxModel(mats));
于 2013-08-15T14:04:56.023 に答える