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 2 of 2

Thread: How to update quantity, +1 upon click on product23 photo, in jtable that is related to jtextfields: name23, price23, quantity23?

  1. #1
    Junior Member
    Join Date
    Aug 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to update quantity, +1 upon click on product23 photo, in jtable that is related to jtextfields: name23, price23, quantity23?

    I want to update the quantity (+1) in jtable cell upon each click on the product photo (product23 the variable)

    private void product23MouseClicked(java.awt.event.MouseEvent evt) {
    String pcode = product23.getText();
    try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/salespos","root","");
    PreparedStatement pst = con.prepareStatement("select * from product where id = ?");

    pst.setString(1, pcode);
    ResultSet rs = pst.executeQuery();

    if(rs.next() == true)
    {
    String pname = rs.getString("name");
    String price = rs.getString("price");
    String quantity = rs.getString("quantity");
    name23.setText (pname.trim());
    price23.setText("$" +price.trim());
    quantity23.setText(quantity.trim());
    byte[] image = rs.getBytes("Image");
    ImageIcon format = new ImageIcon(image);
    product23.setIcon(format);
    }

    } catch (ClassNotFoundException | SQLException ex) {
    Logger.getLogger(pos.class.getName()).log(Level.SE VERE, null, ex);
    }
    DefaultTableModel model = new DefaultTableModel();
    try{
    model = (DefaultTableModel)jTable1.getModel();
    model.addRow(new Object[]
    {
    name23.getText(),
    price23.getText(),
    quantity23.getText(),
    });

    double sum = 0;
    for(int i = 0; i<jTable1.getRowCount(); i++)
    {
    double x ;
    if(jTable1.getValueAt(i, 1).toString().contains("$")){
    x = (double) Double.parseDouble(jTable1.getValueAt(i, 1).toString().substring(1));
    }
    else
    x = (double) (Double.parseDouble(jTable1.getValueAt(i, 1).toString()));

    sum = (double) (sum + x);
    sum = Math.round(sum*100.0)/100.0;
    }
    txtsub.setText("$"+Double.toString(sum));
    }catch(RuntimeException e)
    {
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to update quantity, +1 upon click on product23 photo, in jtable that is related to jtextfields: name23, price23, quantity23?

    Are you asking how to add 1 to a numeric value in a cell in a JTable when a button(???) is pressed?

    What happens when you compile and execute the above code?
    Does it do what you want?
    If it does not do what you want, what do you want to change?


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    --- Update ---

    Also posted at: https://www.dreamincode.net/forums/t...-in-jtable-th/
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: December 13th, 2013, 12:01 AM
  2. Replies: 0
    Last Post: December 1st, 2012, 01:06 AM
  3. [SOLVED] HELP ME! How to update quantity (JAVA)
    By kerrina in forum What's Wrong With My Code?
    Replies: 12
    Last Post: August 2nd, 2011, 03:27 AM
  4. Checkbox doesnt change when click on JTable
    By GodspeedPR in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 02:12 PM
  5. change JTable column name, gui does not update at once
    By renars in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 23rd, 2011, 10:39 AM

Tags for this Thread