7

How can i add data to JTable while working with netbeans. Netbeans in it's back code does like this :

jTable1 = new javax.swing.JTable();
jTable1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null},
            {null, null},
            {null, null},
            {null, null}
        },
        new String [] {
            "Name", "Branch"
        }
    ) {
        boolean[] canEdit = new boolean [] {
            false, false
        };

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    }); // THIS IS THE SNIPPET GENERATED BY NETBEANS
    //( I have already created a table using the drag and drop fetaure of netbeans and this is the back snippet generated)

The 2-D object array and String array have local access , so i can't use to fill it when i want to in the middle of the program. (in some function)

enter image description here

like in the above table i will be adding name and branch while in some function.But how can i do this ?

Can anyone please tell a way so that i can add data to JTable ?

Eric Hydrick
  • 3,467
  • 2
  • 28
  • 41
saplingPro
  • 20,769
  • 53
  • 137
  • 195

4 Answers4

20
jTable1.getModel().setValueAt(value, row, column);
Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
Eric Hydrick
  • 3,467
  • 2
  • 28
  • 41
3

jTable1.getModel().setValueAt(value, row, column);

trejder
  • 17,148
  • 27
  • 124
  • 216
IshanAg24
  • 199
  • 2
  • 11
  • 1
    Please, put any comment to your answer. Code-only answers aren't to valuable and aren't welcome here. Thank you. – trejder Nov 14 '14 at 19:06
1

your question isn't clear for me, but there is basic tutorial about JTable, lots of examples here or here, there is example hoe to add value to the TableCell at Runtime

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0
try {
    pst = con.prepareStatement("select * from emp"); 
    ResultSet rs = pst.executeQuery();
    int i = 0; 
    if (rs.next()) { 
    String  uname = rs.getString("contact_id"); 
    String  email = rs.getString("first_name");
    String  pass = rs.getString("last_name");
    String cou = rs.getString("phone"); 
    model.addRow(new Object[]{uname, email, pass, cou});
    i++;
}
Hiren
  • 1,427
  • 1
  • 18
  • 35