1

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);
       } 
    }                                           
Piro
  • 1,367
  • 2
  • 19
  • 40
Lyetnel
  • 17
  • 1
  • 5
  • From code i would say that you want get value from JTable and use it in sql (and JTextField has nothing to do with this). Please specify what exactly doesn't work - what is wrong? – Piro Aug 15 '13 at 11:50
  • Yeah you understood it true. Exactly the problem is, When i run the program, i clicked to table's a random row;it didnt show me on JTextFields which is next to the table. Here, i can give you a screenshot about that. http://i.imgur.com/BqMJCD6.png – Lyetnel Aug 15 '13 at 11:56
  • So there are blank values in all columns in table, or nothing has been selected from database. Did you print real value of Table_click and did you test that something is selected from database? – Piro Aug 15 '13 at 12:07
  • Meh maybe :) And thanks anyway, mKorbel's answer is true :) – Lyetnel Aug 15 '13 at 12:19

1 Answers1

4
  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());
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thanks man, i conver t them to "jTextField2.setText((rs.getString("Yapılan İşlem")).trim());" and changed JTextField's names as mines , and it WORKS ! ^^ – Lyetnel Aug 15 '13 at 12:19