2013-02-16 103 views
0

出更改默認Colomn名字,我有下面的類:如何ResultSet的數據添加到JTable中使用的JTable

public class customer_master extends javax.swing.JInternalFrame { 

    Connection con = null; 
    PreparedStatement ps = null; 
    ResultSet rs = null; 

    JFrame parent; 
    public customer_master(JFrame parent) { 

     this.parent=parent; 
     initComponents(); 

     try { 
      String qry="select customer_code, customer_name, customer_address, customer_created_time from customer"; 
      database.JavaConnect jc= new JavaConnect(); 
      con = jc.ConnectDB(); 
      ps = con.prepareStatement(qry); 
      rs = ps.executeQuery(); 
      jTable1.setModel(DbUtils.resultSetToTableModel(rs)); 
      con.close(); 
     } 
     catch(SQLException e) 
     { 
      JOptionPane.showMessageDialog(null, e); 
     } 
    }  
} 

它還更改列的名字,但我想添加RS不改變列的名字嗎,如果任何人都可以幫忙。提前致謝。

+0

嗨Kapil。請考慮在您的問題中添加更多信息。你包括什麼課程,目的是什麼?你最終的目標是什麼?什麼是你看到的輸出的例子?您提供的信息越多,其他人就越容易提供幫助。當只有一個班級繼續學習是很困難的。 – 2013-02-16 16:11:24

+0

我承認我沒有提供完整的信息.....我要求一個簡單的解決方案,不會改變默認的jTable列名與數據庫表中的元數據.... // ** DbUtils.resultSetToTableModel(rs)** //這個函數是用表元數據取代默認的列名....但我一直在搜索,我發現這裏的解決方案.... **** http://stackoverflow.com/questions/9599474/jtable-setting -model-and-preserve-column-formats-width-alignment-etc ****謝謝你的支持。非常感謝你.. – 2013-02-17 00:11:40

回答

1

您的問題意味着您正在使用新的ResultSet刷新和現有的JTable。所以,當你創建JTable中第一時間和指定名稱給每個需要添加列:

JTable table = new JTable(...); 
table.setAutoCreateColumnsFromModel(false); 

這將告訴表不重新創建的TableColumnModel因此,所有的列和自定義渲染器和編輯器將保持不變。

+0

謝謝你先生....這是我一直在尋找..非常感謝你..... – 2013-02-17 00:31:29