• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Remove data from a row and update it

 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maneesh

Thanks a lot. I will definitely try and get back to here in case of doubts.
reply
    Bookmark Topic Watch Topic
  • New Topic