Jtable中的数据库,并在所有行的最后一列中添加jcheckbox

时间:2011-12-16 10:24:33

标签: database swing jtable jcheckbox

因为我是初学者,Jtable概念让我的大脑架很多。困难的我已经maganaged读取数据库并将其添加到下面给出的jtable CODE。但我被困在如何在同一个表的最后一列中添加一个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 个答案:

答案 0 :(得分: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方法添加测试。