0

I have populated a jTable from a database, now i want to add more functiaonality, I want to delete a row, i have my data structure like this:

public Vector getUser()throws Exception {

Vector<Vector<String>> userVector = new Vector<Vector<String>>();

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
PreparedStatement pre = conn.prepareStatement("SELECT * FROM korisnici");
 rs = pre.executeQuery();


while (rs.next()) {
    Vector<String> user = new Vector<String>();
    user.add(rs.getString(1)); 
    user.add(rs.getString(2)); 
    user.add(rs.getString(3)); 
    user.add(rs.getString(4)); 
    userVector.add(user);
}

if (conn!=null)
    conn.close();

return userVector;
}

next I created a jTable with model like this

jTable1.setModel(new javax.swing.table.DefaultTableModel(
        data,header
    ));

so now i want to delete row using

jTable1.remove(int);

My db has 7 rows and when i put int=1 i get outOfBoundsException:1

I'm acesing it wrong, right? How do i remove a row, properly?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
mimi87
  • 53
  • 1
  • 9
  • 1
    You add `data` to the `DefaultTableModel`, is vector somehow linked to the `userVector`? I feel like a lot of necessary code is missing. And... [How to remove a row from JTable?](http://stackoverflow.com/questions/1117888/how-to-remove-a-row-from-jtable) – Obicere Oct 08 '14 at 18:39
  • `jTable1.remove(int);` JTable does not have a remove() method. The method you are invoking is the `Container.remove(...)`, which is usually used to remove components from a panel, not a table, since a table doesn't contain any child components. – camickr Oct 08 '14 at 18:56

3 Answers3

2

Use DefaultTableModel for deleting a row based on row number.

((DefaultTableModel) table.getModel()).removeRow(row)

For detail explanation please have a look at How to Use Tables

Example1 and Example2

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
1

To remove a row from the table, you want

((DefaultTableModel)jTable.getModel()).removeRow(rowToRemove);

The JTable itself is really just some display logic: it's the table model underneath that controls the data, and it's that that you need to interact with. The table itself will then render whatever's present in the table model.

Since you're creating the DefaultTableModel yourself, the easier overall way of achieving this would be to set the model via

DefaultTableModel model = new DefaultTableModel(data,header);
jTable1.setModel(model);

and then later on

model.removeRow(rowToRemove);

That saves you querying the table to find the table model every time.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
0

Now i have it working to remove record from JTable, but now I want to remove it from db also, I have it working when i enter a string manually but i want to remove it from db by clicking on it. Here is what i have

PreparedStatement ps = conn.prepareStatement("DELETE FROM user WHERE JMBG = ? ");
         ps.setString(1, value2);
         ps.executeUpdate();

where value2 is a String. How I set value2 to get value from a mouse click?

I got it it works as next:

value2=(String) ((DefaultTableModel) jTable1.getModel()).getValueAt(row, 2);

where row is

int row = jTable1.getSelectedRow();

It was very messy at start, deleteing random stuff, but now it works as intended

mimi87
  • 53
  • 1
  • 9