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 ..
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.
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 ..
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!
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
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
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!
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.
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!
ZDreamer08 (May 22nd, 2013)
okay i'll just work it out first. thanks