简体   繁体   中英

Trying to Get value from “JTable” and and set it to “Jtextfield”

I saw some same questions like mine on here but not all of them including my question.. I want to Get value from "JTable" and and set it to "Jtextfield". I wrote the code, but it doesnt work. Here is my code:

private void Table_EmployeeMouseClicked(java.awt.event.MouseEvent evt)
{                                            
        int row =Table_Employee.getSelectedRow();
        String Table_click=(Table_Employee.getModel().getValueAt(row, 0).toString());
        try{
        String sql ="select * from KYaziciProg where \"Müşteri Adı\"='"+Table_click+"' ";
        pst=conn.prepareStatement(sql);
        rs=pst.executeQuery();
        if(rs.next()){

        String add1 =rs.getString("Yapılan İşlem");
        jTextField2.setText(add1);

        String add2 =rs.getString("Müşteri Adı");
        jTextField1.setText(add2);

        String add3 =rs.getString("Ürünün Cinsi");
        jTextField3.setText(add3);

        String add4 =rs.getString("Ürünün Miktarı");
        jTextField4.setText(add4);

        String add5 =rs.getString("Ürünün Fiyatı");
        jTextField5.setText(add5);

        String add6 =rs.getString("Ürünün Tutarı");
        jTextField6.setText(add6);

        String add7 =rs.getString("İşlem Tarihi");
        jTextField7.setText(add7);

        String add8 =rs.getString("Ödeme Vadesi");
        jTextField8.setText(add8);

        String add9 =rs.getString("Yapılan Ödeme");
        jTextField9.setText(add9);

        String add10 =rs.getString("Kalan Bakiye");
        jTextField10.setText(add10);
        }
       }catch(Exception e){
       JOptionPane.showMessageDialog(null, e);
       } 
    }                                           
  1. add pst.close(); and rs.close(); to finally block ( try - catch - finally ), otherwise these Objects increasing JVM memory

  2. to try to avoid to use MouseListener for this job meaning private void Table_EmployeeMouseClicked(java.awt.event.MouseEvent evt) ,

  3. each of mouse click to invoke JDBC, but there isn't some guarantee

    • any of row is selected, because MouseEvents by default not only to select cell, row or column in JTable

    • only one cell, row or column is selected, otherwise you would need to solve arrays of selected cell, rows, columns

    • added ListSelectionListener

    • changed ListSelectionMode to SINGLE

EDIT

  • use action from JPopup for this job , invoke JDBC from JPopup rather than from Mouse or ListSelectionListerner, then you can use all mouse a key events inside JTable without care of if is JDBC invoked

  • then I miss there testing (otherwise ArraysOf or NPE exception) pseudocode

.

   int row =Table_Employee.getSelectedRow();
   if (row > -1) {  // no selection
     // now is possible to determine value
     // note see my next point about following code line
     // Table_Employee.getModel().getValueAt(row, 0).toString() 
   }

.

.

String add1 = rs.getString("Yapılan İşlem");
jTextField2.setText(add1);

could be with success

jTextField2.setText((rs.getString("Yapılan İşlem")).trim());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM