Not able to display jtable at runtime

Not able to display jtable at runtime

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

}
View Answers

March 8, 2011 at 3:27 PM

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


March 26, 2011 at 1:07 PM

hi i get into a problem of scrolling table in jscrollpane..my vertical scroll is not working..i hope to get an answer

Ads









Related Tutorials/Questions & Answers:
Not able to display jtable at runtime
Not able to display jtable at runtime  Hello, can anyone please help... from database. But it is not creating the table at runtime. It retrieves data from...[][] taskcells; JTable ttable; public void BuildTable() { Statement st; ResultSet rs
Not able to filter rows of jtable with textfield in netbeans
Not able to filter rows of jtable with textfield in netbeans  ...", "Item", "Qty In Boxes"}; tblStock = new JTable(data,header); sorter=new... of implementing row filter to the 4th column of jtable with a textfield but its
Advertisements
Not able to filter rows of jtable with textfield in netbeans
Not able to filter rows of jtable with textfield in netbeans  ...", "Item", "Qty In Boxes"}; tblStock = new JTable(data,header); sorter=new... of implementing row filter to the 4th column of jtable with a textfield but its
Display both image and data into Swing JTable
Display both image and data into Swing JTable  How To Display both image and data into Swing JTable which is retrieved from MySQL database
how to sort the text file and display it in jtable
how to sort the text file and display it in jtable   my text file... ctgaggactcctcg i want to display the lines started with ">" symbol to be displayed in jtable and sequence like "CGGGAATTAA..." in jtextarea in netbeans . pls suggest
JTable
JTable   how to select a definite cell which containing a similar text containg to the one which the user entering from a jtable at runtime in java
I am not able to display the selected value of my combobox
I am not able to display the selected value of my combobox   <?php echo "<select name=\"hello\">\n"; echo "<option value=\"NULL\">Select Value</option>\n"; $strQuery = "select unitcode,unitname from
JTable
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
Select Employee and display data from access database in a jtable
Select Employee and display data from access database in a jtable  I there Iam a java beginner. I have to create a application where I can select a employee's name from a comboBox and the jtable will be filled with all
How to collect Java input field value display into Jtable?
How to collect Java input field value display into Jtable?  <p>We would like to know how to collection input field data into Jtable... as the following information. - display the value in the JTable. If in the GUI as per view
jtable
jtable  hey i have build a form and i m also able to add data from database to jtable along with checkbox.the only problem is that if i select multiple checkboxes the data doesnt get inserted into new database and if only one
JTable
JTable  Values to be displayed in JTextfield when Clicked on JTable Cells
JTable
JTable  i want to delete record from JTable using a MenuItem DELETE. and values of JTable are fetched from database....please reply soon
JTable
JTable  Hi I have problems in setting values to a cell in Jtable which is in a jFrame which implements TableModelListener which has a abstract.... And i'm not able to set value to a particular column. How can i do it? Please
jtable
jtable  how to get the values from database into jtable and also add a checkbox into it and then when selected the checkbox it should again insert into database the selected chewckbox.plzz help
jtable
jtable  hi Sir i am working netbeans IDE,I have a jtable when i insert values in jtable then i am unable to print all inserted values,For eg if i insert 1,2,3,4,5,6,7,8 values then , i am getting output
How To Display both image and data into Swing JTable which is retrieved from ms access database
How To Display both image and data into Swing JTable which is retrieved from ms access database  So far this is my code how can i display both image and data from database.. while (rs.next()) { Vector row = new Vector(columns
JTable
"}; JTable table=new JTable(data,labels); JScrollPane pane=new JScrollPane
Create a Custom Cell Renderer in a JTable
that can be used to display the cells in a column. The JTable describes a specific... Create a Custom Cell Renderer in a JTable       After getting the knowledge about the JTable
all sequences to create jdbc and how can i use jtable to display a single columns or multiple columns
all sequences to create jdbc and how can i use jtable to display a single... a view in my database schema so how can i create a jtable to look the view result... ); } } catch(Exception e){} JTable table = new JTable(data, columnNames); JScrollPane
jtable
jtable  i have build an application and i retrieve data from the database and store it in jtable.now i have to make a checkbox column in each row and also a select all option in header but i am not able to do so.i am getting
java jtable
in which i have to display database records in jtable .now I want to read only the values of particular row at which the jtextfield has been set. and display that in jtable. plz help me with the code
Show multiple identical rows into JTable from database
Show multiple identical rows into JTable from database In this tutorial, you will learn how to display the multiple rows from database to JTable. Here... rows from database on clicking search button to jtable. The given code accepts
JTable Display Data From MySQL Database
JTable Display Data From MySQL Database In this section we will discuss about how to display data of a database table into javax.swing.JTable This section will describe you the displaying of data of a database table into a JTable
regarding jtable...
regarding jtable...  sir, im working with jtables. i wanted to populate a jtable from the database and when i click any row it should add a container... table after clicking the row on to the jdialog box. i am not able to add
display
display  please tell me how to display the content from database.. if we click on any image using servlets/jsp...please
Jbutton in JTable cells
Jbutton in JTable cells  I am a Java Beginner... I want a code in which I display data in Jtable from database and correspondingly a JButton for each cell and when i click on that button a new window should open
Swing JTable which is retrieved from MySQL
Swing JTable which is retrieved from MySQL  How To Display both image and data into Swing JTable which is retrieved from MySQL database
jtable query compare with date
in which i have to display database records in jtable .now I want to transfer particular data which month is over to another jtable. plz sir give me the code...jtable query compare with date  how to transfer daytable data
jtable query compare with date
in which i have to display database records in jtable .now I want to transfer particular data which month is over to another jtable. plz sir give me the code...jtable query compare with date  how to transfer daytable data
jtable query compare with date
in which i have to display database records in jtable .now I want to transfer particular data which month is over to another jtable. plz sir give me the code...jtable query compare with date  how to transfer daytable data

Ads