Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Im having a problem in retrieving values from jTable.

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Location
    Philippines
    Posts
    19
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Im having a problem in retrieving values from jTable.

    how can i get the value from a jtable? im only retrieving one row value. but it always display an error which says 3>=3 ..


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Im having a problem in retrieving values from jTable.

    Can you provide an SSCCE that demonstrates exactly what you're trying to do? Note that this should be as small as possible (not your whole program) but still be runnable (not just a snippet).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Location
    Philippines
    Posts
    19
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Im having a problem in retrieving values from jTable.

    Sorry. idk how to cut it. This is the simplest code that will be understood i guess. but i'll provide the output and error

     
    String getRowValue=String.valueOf(tblmodel.getValueAt(2, 2).toString());
            System.out.println("CHECK VALUE: "+getRowValue);



    THIS IS THE OUTPUT.
    Untitled.jpg

    First you have to choose from the combobox.
    then the price will be displayed on the textfield beside jlabel price.
    then you have to input quantity of product. once you hit enter key, the values will be displayed on the jtable.
    all the TOTAL PRICES will be displayed on the texfield beside jlabel total.


    and this is the error:

    ew.jpg

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Im having a problem in retrieving values from jTable.

    That snippet of code is pretty useless without knowing what's already in your table. How are you adding the data? Can you create a simple hard-coded table with the same number of rows and columns, then read form that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Location
    Philippines
    Posts
    19
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Im having a problem in retrieving values from jTable.

    I hope you understand
    this is how i add row for the jTable.


    THIS CODE IS IS IN THE GUI. WHEN I HIT ENTER KEY ON QUANTITY TEXTFIELD. THIS WILL PROCESS.

     
    String quantity = jTextFieldQuantity.getText();
            String selectItem = jComboBoxChoose.getSelectedItem().toString();
            String stock = crud.productStock(selectItem,quantity,sqlSelect);
            double totalPrice=0;
            int quant = Integer.parseInt(quantity);
            tblmodel = crud.addSales(tblmodel, selectItem, quant, totalPrice);


    THE METHOD PRODUCTSTOCK CHECKS THE STOCK FROM THE DATABASE. WHETHER IT IS NOT ENOUGH OR VICE VERSA.

     
        public String productStock(String select, String getQuant, String sq){
            System.out.println("Entered quantity of product: "+getQuant);
            int diff;
           try {
               st=con.createStatement();
               rs=st.executeQuery(sq);
     
               while(rs.next()){
     
                   if(select.equalsIgnoreCase(rs.getString(1))){
                       System.out.println("Check stocks "+select.toUpperCase()+" from DB: "+rs.getString(2));
                       int n1=Integer.parseInt(getQuant);
                       int n2=Integer.parseInt(rs.getString(2));
     
                        if(n1<n2){
                            diff=n2-n1;
    //                        String update="insert into product values('"+select+"',"+diff+","+rs.getString(3)+")";
                            String update="UPDATE PRODUCT set Quantity="+diff+" where ProductName='"+select+"'";
                            st.executeUpdate(update);
     
                        }
                        else if(n1>n2){
                            JOptionPane.showMessageDialog(null, "Not enough stocks! STOCKS left: "+rs.getString(2));
                        }
     
                        else if(n1==n2){
                             diff=n2-n1;
    //                        String update="insert into product values('"+select+"',"+diff+","+rs.getString(3)+")";
                            String update="UPDATE PRODUCT set Quantity="+diff+" where ProductName='"+select+"'";
                            st.executeUpdate(update);
                        }
                   }
               }
           } catch (SQLException ex) {
           }
            return getQuant;
        }//PRODUCTSTOCK METHOD

    NOW THIS IS THE CODE WHERE I ADD ROWS.

     
        public DefaultTableModel addSales(DefaultTableModel tmodel, String ProdName, int Quantity, double Price){
           Object[] save=new Object[3];
     
     
            try {
               st=con.createStatement();
               rs=st.executeQuery("Select*from product");
     
               while(rs.next()){
                   if(ProdName.equalsIgnoreCase(rs.getString(1))){
     
                       if(Quantity>rs.getInt(2)){
                           System.out.println("EXCEED!");
                       }
     
                       else if(Quantity==0){
                           JOptionPane.showMessageDialog(null, "Enter quantity");
                       }
     
                       else{
                       Double sum=Quantity*rs.getDouble(3);
                       save[0]=ProdName;
                       save[1]=Quantity;
                       save[2]=sum;
                       tmodel.addRow(save);
     
     
                       }
     
                   }
     
               }
     
           } catch (SQLException ex) {
    //           Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, ex);
           }
     
     
            return tmodel;
        }//ADDSALES METHOD


    THANKS FOR HELPING ME

    --- Update ---

    PS. WHEN RETRIEVING VALUES, I DONT NEED TO ACCESS DATABASE, ONLY THE ROWS FROM THE THIRD COLUMN.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Im having a problem in retrieving values from jTable.

    I recommend you hardcode an example table with the same number of values your real table has. You only need to add a couple rows. Then use your code for retrieving the values on that example table and see what happens. If the problem persists, you'll have an SSCCE that we can look at to help you out.

    It's really up to you, but we can't really help by looking at disconnected snippets of contextual code. Give us an example we can work with, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    ZDreamer08 (May 22nd, 2013)

  8. #7
    Junior Member
    Join Date
    Apr 2013
    Location
    Philippines
    Posts
    19
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Im having a problem in retrieving values from jTable.

    okay i'll just work it out first. thanks

Similar Threads

  1. retrieving hashmap data in table format based key values
    By swapnareddy in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: April 16th, 2013, 08:48 AM
  2. JTable Problem
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 27th, 2013, 02:23 PM
  3. Problem of retrieving pdf file from database using tomcat server
    By kalkumbenitin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 26th, 2012, 06:16 AM
  4. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM
  5. problem in retrieving data via RMS
    By solaleh in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: August 30th, 2009, 05:46 AM

Tags for this Thread