Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Remove data from a row and update it  RSS feed

 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a client server application. The UI for the server contains a JTable and in each row, details of the connected client are shown. Say if I have 5 connections and the 3rd connection disconnects, I need the 4th and 5th connection details to be displayed in 3rd and 4th row respectively. Can this be done?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JTable will rely on a backing model for the data. The DefaultTableModel provides a convenient removeRow(int row); method which you can use.
More on using JTable here http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh

Thanks for the reply. The coding for the table was done by another person and he has used AbstractTableModel for it. I think I have to custom code the removal of rows in that case right?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:Maneesh
Thanks for the reply. The coding for the table was done by another person and he has used AbstractTableModel for it. I think I have to custom code the removal of rows in that case right?

Yes.

This is the actual code from DefaultTableModel#removeRow(int row);


You can provide a similar method to your table model. Just ensure you fire the fireTableRowsDeleted(row, row) which will subsequently for the fireTableChanged() which will result in your table (view) being updated automatically.

You can also consider extending the DefaultTableModel instead of the abstract one.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh

As I had mentioned earlier, the code is from another person and he totally doesn't like the changes I do. So changing the existing code is out of options for me. In the code you have provided, may I know what dataVector is?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As I mentioned earlier, it is the actual code from DefaultTableModel#removeRow(int row); i.e. the source code for the java library. You can check the internal workings of the class by opening it. All java source code can be found bundled inside a src.jar under your JAVA_HOME directory
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh

Thanks for the reply. I will try it and get back here if there is further more doubts. I have one more doubt, will removing the row automatically update the contents? That is if I remove nth row will the (n+1)th row become the nth row automatically?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:..will removing the row automatically update the contents? That is if I remove nth row will the (n+1)th row become the nth row automatically?

Yes...
Provided your model returns the getRowCount() & getValueAt() correctly, and
Maneesh Godbole wrote:Just ensure you fire the fireTableRowsDeleted(row, row) which will subsequently for the fireTableChanged() which will result in your table (view) being updated automatically.

 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh

Thanks a lot. I will definitely try and get back to here in case of doubts.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!