Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set value in jtable fields??

 
Rahul Surati
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Friends,
I want to set values in to the jtable from the database.
I code something. I am sending this code . please send me modification...

code:

sel = "select A.st_id,A.name,A.adm_year, B.crrt_year,C.mcode,C.grade,C.credit," +
"C.attempt from student_master A , student_sub B, grade_master C" +
" where (A.st_id=B.st_id) and (A.st_id=C.st_id) and A.st_id='" + jTextField1.getText() + "'";
rs1 = s.executeQuery(sel);
System.out.println(sel);
Object k[][] = null;

while (rs1.next()) {
//System.out.println(sel);
//int z = ;
//System.out.println(z);
k= new Object[8][8];
//jTable1.setModel(new DefaultTableModel(k, new String[]{"Student ID", "Name", "Admmission Year", "Current Year", "Modules", "Grade", "Credit", "Attempt"}));
//for (int i = 0; i < z; i++) {
int i=0;
//System.out.println(sel+i);
//for(int j=0;j<8;j++){
//jTable1.setValueAt(rs1.getString("name"), i, j);
k[i][0] = rs1.getString("st_id");
k[i][1] = rs1.getString("name");
k[i][2] = rs1.getString("adm_year");
k[i][3] = rs1.getString("crrt_year");
k[i][4] = rs1.getString("mcode");
k[i][5] = rs1.getString("grade");
k[i][6] = rs1.getString("credit");
k[i][7] = rs1.getString("attempt");
System.out.println(k[i][4]);
i++;

// }
// }

}
jTable1.setModel(new DefaultTableModel(k, new String[]{"Student ID", "Name", "Admmission Year", "Current Year", "Modules", "Grade", "Credit", "Attempt"}));
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> please send me modification...

OK, use code tags
 
Rahul Surati
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:> please send me modification...

OK, use code tags


What ??
I am not getting you ..
Please explain me..
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Please explain me..

do you write code in the format you've posted for us to read?

i.e. which is easier to read?

class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}

or (using code tags)

 
Rahul Surati
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:> Please explain me..

do you write code in the format you've posted for us to read?

i.e. which is easier to read?

class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}

or (using code tags)


this code is in the method of button action perform event ..
now you will get it..
when i press button table may set value to their fields...
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael was referring to UseCodeTags.
 
Rahul Surati
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You create a completely new data grid each loop iteration, while you should just add one extra grid row.

Try creating a DefaultTableModel with only the headers and 0 rows before the loop (new DefaultTableModel(..., 0)), then call addRow on that DefaultTableModel within the loop body. Afterwards you use setModel with that DefaultTableModel instance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic