We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,379 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

JComboBox Wont Select Item Selected

only the first item in the combobox will be returened even if i selected another item.
HELP!

try{
            //combobox of the products
            rs = stmt.executeQuery(query2);
            rs.last();
            int i = rs.getRow();
            String [] v = new String [i];
            rs.beforeFirst();
            i = 0;
            while(rs.next())
            {
                v[i] = rs.getString(2);
                i++;
                //break;
            }

             prodna = new JComboBox(v);
        }
        catch(Exception e)
        {

        }
        
        getContentPane().add(prodna);
        srch = prodna.getSelectedItem().toString();
        //ordNo = new JLabel(srch);
        ordNo.setText(srch);

        label_5 = new JLabel("Product Name:");      //***Label
        getContentPane().add(label_5);

        label_6 = new JLabel("Quantity");            //***Label
        getContentPane().add(label_6);

        gqty = new JTextField("");
        getContentPane().add(gqty);

        ok = new JButton("OK");
        getContentPane().add(ok);

        cancel = new JButton("Cancel");
        getContentPane().add(cancel);

        setSize(getPreferredSize());

        ok.addActionListener(new ActionListener(  ) {
            public void actionPerformed(ActionEvent ae) {


                        try {
                            rs = stmt.executeQuery(query2);
                            rs.beforeFirst();
                                    while(rs.next()){
                                        String pna = rs.getString("prodNa");
                                        String pnum = rs.getString("prodNo");
                                        String qty1 = rs.getString("qty");
                                        int qty11 = Integer.parseInt(qty1);;
                                        String price = rs.getString("unitPrice");
                                         y = Integer.parseInt(gqty.getText());
                                         tprice = Float.parseFloat(price);
                                         if (pna.equalsIgnoreCase(srch.trim()))
                                        {
                                            System.out.print(pna);
                                            System.out.println(tprice);
                                            System.out.println(y);
                                            System.out.println(x);
                                            System.out.println(pnum);
                                            tp = (int) (y * tprice);
 
                                            stmt.executeUpdate("INSERT INTO `orderdetails` (`orderNo`,`prodNo`,`qty`, `payable`)  VALUES('"+x+"','"+pnum+"','"+y+"','"+tp+"');");
                                            pqty = qty11 - y;
                                            stmt.executeUpdate("UPDATE `products` SET `qty` = 'pqty' WHERE `products`.`prodNo` =1005 ;");
                                            break;

                                         }

                                    }
                        } catch (SQLException ex) {
                            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                        }

                            setVisible(false);
                            newOrder n = new newOrder();
                            n.setVisible(true);
                            n.setLocation(500, 200);
            }

                });
2
Contributors
1
Reply
4 Hours
Discussion Span
1 Year Ago
Last Updated
2
Views
sahmeme
Newbie Poster
2 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

1) don't create JComboBox inside Databases Connection, create this JComponent before connection,

2) put this data to the Vector, inside ResultSet loops only add myVector.add(rs.getString(2)),

3) test if isn't rs.getString(2) null or empty, because emty JComboBoxes Item doesn't looks like nice....

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0564 seconds using 2.67MB