2011-12-16 58 views
0

因爲我是初學者,Jtable概念使我的大腦架了很多。與困難我maganaged閱讀一個數據庫,並將其添加到下面給出的jtable代碼。但我堅持如何在同一張表的最後一列添加JCHECKBOX。Jtable中的數據庫並在所有行的最後一列中添加jcheckbox

public class ttt extends JFrame{ 
ResultSet rs; 
int colcount; 
String[] headers; 
Connection con; 
Statement st; 
ttt(){ 
final Vector columnNames = new Vector(); 
    final Vector data = new Vector(); 
    JPanel panel=new JPanel(); 
    try{ 
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
     System.out.println("Driver loaded"); 

     // Establish a connection 
     con= DriverManager.getConnection 
      ("jdbc:odbc:ysr"); 
     System.out.println("Database connecteddddd"); 

     // Create a statement 
     st = con.createStatement(); 
     ResultSet rs = st.executeQuery("SELECT 
    Block_Name,Panchayat_Name,Village_Name," + 
       " Habitation_Name,Scheme_Name,Sanction_Amount FROM 

ysr2011 where Habitation_Name= '10th mile' "); 
ResultSetMetaData md = rs.getMetaData(); 

int columns = md.getColumnCount(); 
for (int i = 1; i <= columns; i++) { 
columnNames.addElement(md.getColumnName(i)); 
} 
while (rs.next()) { 
Vector row = new Vector(columns); 
for (int i = 1; i <= columns; i++) { 
row.addElement(rs.getObject(i)); 
} 
data.addElement(row); 
} 
} 
catch(Exception e){} 
JTable table = new JTable(data, columnNames); 
JScrollPane scrollPane = new JScrollPane(table); 
panel.add(scrollPane); 
add(panel); 
} 
public static void main(String arg[]) 
{ 
try 
{ 
ttt frame=new ttt(); 
frame.setSize(550,200); 
frame.setVisible(true); 
} 
catch(Exception e) 
{} 
} 
} 

任何幫助確實是一個歡迎的手勢。提前預覽。

+1

請學習java命名約定,並堅持他們 – kleopatra 2011-12-20 15:51:00

回答

1

你會在這裏添加複選框:

int columns = md.getColumnCount(); 
for (int i = 1; i <= columns; i++) { 
    columnNames.addElement(md.getColumnName(i)); 
} 
columnNames.addElement("Check Box"); 
while (rs.next()) { 
    Vector row = new Vector(columns + 1); 
    for (int i = 1; i <= columns; i++) { 
     row.addElement(rs.getObject(i)); 
    } 
    row.addElement(new JCheckBox()); 
    data.addElement(row); 
} 

使用您自己的列標題。您還必須更加完整地定義複選框,因爲最終您將爲isSelected方法添加測試。

+0

感謝代碼只是爲我工作。 – user1101703 2011-12-26 07:35:15

相關問題