2

hi I have this jtable which I want to have its auto increment row header for the user to identify how many data does the table returned. I am actually getting the table model from result set and the data is not fixed. It vary depends on the search of the user.

Here's my table model code:

public void retrieveMember() throws SQLException {
     mDao.dbConnect();
        try {

        if(mDao.con!=null)
        {  
   mDao.ps = mDao.con.prepareStatement(this.getSql());
   mDao.rs = mDao.ps.executeQuery();
   this.tblGender.setModel(DbUtils.resultSetToTableModel(mDao.rs));

    int[] columnsWidth = { 100, 150, 150, 300, 50, 60, 100, 100, 125,65};
    int i = 0;
    for (int width : columnsWidth) {
        TableColumn column =this.tblGender.getColumnModel().getColumn(i++);
        column.setMinWidth(width);
        column.setMaxWidth(width);
        column.setPreferredWidth(width);
    }

   } else
        {
              System.out.println("Con is null");
        }

    } catch (SQLException ex) {
        ex.printStackTrace();
        throw ex;


    }
  }

Can anyone help me to put an auto increment row header? Something that will display how many data does my table returning.

            -------------------------
                  Name| Age | Gender
            -------------------------
               1| Nely| 16  |Female
               2| Amy | 18  |Female

thank you in advance.

Jason C
  • 38,729
  • 14
  • 126
  • 182
miles
  • 225
  • 3
  • 10
  • 20

2 Answers2

4

Perhaps one of these options, if modifying the original table model is not possible:

I recommend the first option. It is straightforward and can be used in many situations.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • How am I going to attach the row header code in my program? Can you show some example? I dont know where to start. Anyway I just dragged the table from swing. I did not code it manually like Jtable tblGender = new JTable. So I dont know what to modify – miles Nov 19 '13 at 07:37
  • Those examples include code to attach the row header to an existing `JTable` by calling `setRowHeaderView` on the enclosing `JScrollPane`. I do not know what IDE/GUI editor you are using, but take a look at the code it generates. Most GUI editors don't have a problem if insert your own code there (for example, in Eclipse w/ WindowBuilder, you can just add code to the generated GUI class, the editor won't choke on it). – Jason C Nov 19 '13 at 07:48
  • I am using Netbeans and theres no setRowHeaderView here. – miles Nov 19 '13 at 08:00
  • I found the setRowHeaderView here in the JScrollPane. what can I put here to generate an autonumber rowheader? – miles Nov 19 '13 at 08:02
  • 1
    I'm not sure what else to tell you. Those examples are very clear. Add the relevant row header classes to your source, then install them by calling `setRowHeaderView` on the `JScrollPane` in your generated code. If you don't have a scroll pane, add one and put your table in it. There's not much more to it. – Jason C Nov 19 '13 at 08:05
0

Try this :

If number of columns are fix for your table then you can take the counter to get the total number of rows return by table in database.

 mDao.rs = mDao.ps.executeQuery();
 int count = 0;

    /** Setting table fields values **/
    if( mDao.rs.next())
        table.setValueAt( mDao.rs.getString("Name"),count,0);
        table.setValueAt(mDao.rs.getInt("Age"),count,1);
        table.setValueAt(mDao.rs.getString("Gender"),count,2);
        count++;
    }

Count will give the no of record return by table.

rachana
  • 3,344
  • 7
  • 30
  • 49