
Hello,
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();
}
}

Oh..I forgot to set panel's layout.Now its working..

hi i get into a problem of scrolling table in jscrollpane..my vertical scroll is not working..i hope to get an answer
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.