如何使用jtable第一列中的复选框显示从数据库到jtable的数据? [英] How to show data from database to jtable with checkboxes in the first column in jtable?
问题描述
我知道如何填充jtable,并且我已经为上面的代码准备了可以正常工作的代码.但是问题在于它正在另一个项目中运行,但在我当前的项目中却是机器人.这两个项目的代码是相同的.这里是代码... purchaswtab是已经使用swing面板创建的表名.并且,在项目主文件"的退出按钮的情况下执行此功能. 定义数组ap的数组元素是因为我必须从结果集中的给定位置获取它们.
I know how to populate jtable and i had prepared the code for the above which works fine. But the problem is that it is working in another project but bot in my current project. The code for both the project is same. Here is the code... purchaswtab is the table name which has been already created using swing palette. And this function is executed in the event of the exit button of the Item Master. the array elements of an array ap are defined as i have to fetch them from the given position from the resultset.
public void populatetable(ItemMaster imm)
{
imm.dispose();
String t[][]=new String [30][10];
int[] ap={19,1,2,4,18,16,17};
Object[] h1=new Object[7];
String a1,a2;
int j=0,i=1;
int len=0;
try
{
st=fun.connect1();
String query="select * from Temp_Purchase";
ResultSet rs=st.executeQuery(query);
ResultSetMetaData rsmd=rs.getMetaData();
while(rs.next())
{
int k=0;
for(i=1;i<=7;i++,k++ )
{
t[j][i-1]=rs.getString(ap[k]);
System.out.println(t[j][i-1]);
}
j++;
len++;
}
fun.close();
}
catch(Exception e)
{
fun.close();
System.out.println("Exception:"+e);
}
DefaultTableModel de=(DefaultTableModel)purchaseTab.getModel();
purchaseTab=new JTable(de);
//Code for filling data into table
for(i=0;i<len;i++)
{
for(j=1;j<=7;j++)
{
h1[j-1]=t[i][j-1];
}
if(h1[0]=="0")
{
h1[0]=Boolean.FALSE;
}
de.insertRow(i,h1);
}
//jsp.setViewportView(Table);
}
推荐答案
-
将
while(rs.next())
内部的所有东西替换为de.addRow(new Object[..., ..., ...]);
replace evetything inside
while(rs.next())
tode.addRow(new Object[..., ..., ...]);
您必须将
DefaultTableModel de
声明为局部变量,并在调用fun
之前对其进行初始化you have to declare
DefaultTableModel de
as local variable, initialized beforefun
is called然后将
DefaultTableModel de
传递给JTable
,例如myTable(de);
或myTable.setModel(de)
then to pass
DefaultTableModel de
toJTable
, e.g.myTable(de);
ormyTable.setModel(de)
使用
Object[]
而不是String[]
的限制,那么您可以use
Object[]
instead of limitations forString[]
, then you can to store various data type inDefaultTableModel
这篇关于如何使用jtable第一列中的复选框显示从数据库到jtable的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!