File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to set value in jtable fields?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to set value in jtable fields??" Watch "How to set value in jtable fields??" New topic
Author

How to set value in jtable fields??

Rahul Surati
Greenhorn

Joined: Mar 04, 2009
Posts: 20
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

Joined: Jun 09, 2003
Posts: 4632
> please send me modification...

OK, use code tags
Rahul Surati
Greenhorn

Joined: Mar 04, 2009
Posts: 20
Michael Dunn wrote:> please send me modification...

OK, use code tags


What ??
I am not getting you ..
Please explain me..
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Mar 04, 2009
Posts: 20
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

Joined: Oct 27, 2005
Posts: 19785
    
  20

Michael was referring to UseCodeTags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rahul Surati
Greenhorn

Joined: Mar 04, 2009
Posts: 20
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set value in jtable fields??