带有复选框问题的 JTable - 运行时未创建表
谁能帮助我吗?我正在尝试创建一个带有复选框的动态表。数据是从数据库访问的。但它不是在运行时创建表。它正确地从数据库检索数据。但我不明白为什么它不在指定的面板(panTable)中创建表。下面是代码:
int tasktotr=0;
Object[][] taskcells;
JTable ttable;
public void BuildTable() {
Statement st;
ResultSet rs;
panTable.updateUI();
String query="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:dsndbPMA","","");
st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
query="select * from Task";
rs=st.executeQuery(query);
rs.last();
tasktotr=rs.getRow();
System.out.println("tasktotr="+tasktotr);
taskcells=new Object[tasktotr][6];
if(tasktotr>0){
int cnt=0;
rs.beforeFirst();
for(int c=0;c<6;c++){
if(c==cnt){
int r=0;
while(rs.next()){
if(c==0)
taskcells[r][c]=Boolean.FALSE;
else if(c==1)
taskcells[r][c]=rs.getString("Priority");
else if(c==2)
taskcells[r][c]=rs.getString("Subject");
else if(c==3)
taskcells[r][c]=rs.getString("Status");
else if(c==4)
taskcells[r][c]=rs.getString("DueDate");
else if(c==5)
taskcells[r][c]=rs.getString("%Complete");
else if(c==6)
taskcells[r][c]=rs.getString("Category");
r++;
}
}
rs.beforeFirst();
cnt++;
}
rs.close();
st.close();
for(int i=0;i<tasktotr;i++){
for(int j=0;j<6;j++){
System.out.println(taskcells[i][j]);
}
}
TableModel tasktable=new TaskTableModel(tasktotr,7);
ttable=new JTable(tasktable);
TableColumnModel columnModel=ttable.getColumnModel();
TableColumn column;
for(int i=0;i<ttable.getColumnCount();i++) {
column = columnModel.getColumn(i);
if(i==0)
column.setPreferredWidth(40);
if(i==1)
column.setPreferredWidth(40);
if(i==2)
column.setPreferredWidth(150);
}
ttable.setRowHeight(18);
ttable.setAutoResizeMode(ttable.AUTO_RESIZE_OFF);
ttable.getTableHeader().setReorderingAllowed(false);
JScrollPane scroll=new JScrollPane(ttable);
scroll.setAutoscrolls(true);
panTable.add(scroll);
panTable.updateUI();
panTable.revalidate();
}
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,e.getMessage(),"Error!!",1);
}
}
class TaskTableModel extends AbstractTableModel{
int totrow,totcol;
public TaskTableModel(int r,int c){
totrow=r;
totcol=c;
}
@Override
public int getRowCount() {
return totrow;
}
@Override
public int getColumnCount() {
return totcol;
}
public Object getValueAt(int r,int c) {
return taskcells[r][c];
}
public String getColumnName(int c){
if(c==0)
return "Icon";
else if(c==1)
return "Priority";
else if(c==2)
return "Subject";
else if(c==3)
return "Status";
else if(c==4)
return "Due Date";
else if(c==5)
return "% Completed";
else if(c==6)
return "Category";
else
return "";
}
public void setValueAt(Object obj,int r,int c){
taskcells[r][c]=obj;
fireTableCellUpdated(r, c);
}
//Edit only first column.
public boolean isCellEditable(int r,int c){
return c==0;
}
//used to display checkbox else it will show true/false text instead of checkbox. n
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
can anyone please help me. I am trying to create a dynamic table with checkboxes. Data are accessed from database. But it is not creating the table at runtime. It retrieves data from database properly. But I am not getting why it is not creating the table in a specified Panel (panTable). Below is the code:
int tasktotr=0;
Object[][] taskcells;
JTable ttable;
public void BuildTable() {
Statement st;
ResultSet rs;
panTable.updateUI();
String query="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:dsndbPMA","","");
st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
query="select * from Task";
rs=st.executeQuery(query);
rs.last();
tasktotr=rs.getRow();
System.out.println("tasktotr="+tasktotr);
taskcells=new Object[tasktotr][6];
if(tasktotr>0){
int cnt=0;
rs.beforeFirst();
for(int c=0;c<6;c++){
if(c==cnt){
int r=0;
while(rs.next()){
if(c==0)
taskcells[r][c]=Boolean.FALSE;
else if(c==1)
taskcells[r][c]=rs.getString("Priority");
else if(c==2)
taskcells[r][c]=rs.getString("Subject");
else if(c==3)
taskcells[r][c]=rs.getString("Status");
else if(c==4)
taskcells[r][c]=rs.getString("DueDate");
else if(c==5)
taskcells[r][c]=rs.getString("%Complete");
else if(c==6)
taskcells[r][c]=rs.getString("Category");
r++;
}
}
rs.beforeFirst();
cnt++;
}
rs.close();
st.close();
for(int i=0;i<tasktotr;i++){
for(int j=0;j<6;j++){
System.out.println(taskcells[i][j]);
}
}
TableModel tasktable=new TaskTableModel(tasktotr,7);
ttable=new JTable(tasktable);
TableColumnModel columnModel=ttable.getColumnModel();
TableColumn column;
for(int i=0;i<ttable.getColumnCount();i++) {
column = columnModel.getColumn(i);
if(i==0)
column.setPreferredWidth(40);
if(i==1)
column.setPreferredWidth(40);
if(i==2)
column.setPreferredWidth(150);
}
ttable.setRowHeight(18);
ttable.setAutoResizeMode(ttable.AUTO_RESIZE_OFF);
ttable.getTableHeader().setReorderingAllowed(false);
JScrollPane scroll=new JScrollPane(ttable);
scroll.setAutoscrolls(true);
panTable.add(scroll);
panTable.updateUI();
panTable.revalidate();
}
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,e.getMessage(),"Error!!",1);
}
}
class TaskTableModel extends AbstractTableModel{
int totrow,totcol;
public TaskTableModel(int r,int c){
totrow=r;
totcol=c;
}
@Override
public int getRowCount() {
return totrow;
}
@Override
public int getColumnCount() {
return totcol;
}
public Object getValueAt(int r,int c) {
return taskcells[r][c];
}
public String getColumnName(int c){
if(c==0)
return "Icon";
else if(c==1)
return "Priority";
else if(c==2)
return "Subject";
else if(c==3)
return "Status";
else if(c==4)
return "Due Date";
else if(c==5)
return "% Completed";
else if(c==6)
return "Category";
else
return "";
}
public void setValueAt(Object obj,int r,int c){
taskcells[r][c]=obj;
fireTableCellUpdated(r, c);
}
//Edit only first column.
public boolean isCellEditable(int r,int c){
return c==0;
}
//used to display checkbox else it will show true/false text instead of checkbox. n
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它无法正确显示的原因是
getValueAt
方法中的ArrayIndexOutOfBoundsException
。表taskcells
创建有六列(也只有六列被填充并显示在打印循环中),但表模型创建有七列。如果您更正它,您的代码应该可以工作(尽管您未在此处发布的代码部分中可能仍然存在问题)。注意:您可以考虑从数据库中读取数据,而不是按列而是按行,即重新排列
rs
和c
循环。然后你甚至可以完全省略c
循环。The reason why it isn't displayed correctly is an
ArrayIndexOutOfBoundsException
within yourgetValueAt
method. The tabletaskcells
is created with six columns (also only six are filled and displayed in your print loop) but the table model is created with seven columns. If your correct it your code should work (although there might still be an issue within a code part you didn't post here).Note: You may consider reading the data not column-wise but row-wise from the database, i.e. rearrange the
rs
andc
loops. Then you can even omit thec
-loop completely.