Results 1 to 17 of 17
  1. #1
    hacker9116 is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default JTable Question?

    hi i just a quick question about a jtable here it is: i have a Jtable that is being filled with rows and column from my database. when i enter an a users username and i click delete it will delete the user from the database. the thing is it wont show the change in the jtable unless i refresh the gui. so my question is is there a way to when i delete a row in the database the jtable is automatically updated so u see the change there and then? any help would be much appreciated thank you.

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You remove rows from the model of the JTable. Doing this, it is supposed to automatically update the GUI.
    Java Code:
    DefaultTableModel model = (DefaultTableModel)jtable.getModel();
    model.remove(rowNum);

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ra4king View Post
    You remove rows from the model of the JTable. Doing this, it is supposed to automatically update the GUI.
    Java Code:
    DefaultTableModel model = (DefaultTableModel)jtable.getModel();
    model.remove(rowNum);
    To the OP, is this the solution to your problem, or is your problem more of being able to listen to the database for changes to occur, kind of like a PropertyChangeListener but for the database, and when this happens trigger changes in the JTable's displayed data? If the latter, I think that NetBeans has bundled in it a way to bind the table's model to the database, but I've never used it. Another way is to intermittently poll the database, looking for changes and if found, updating the model and firing the appropriate fireTableXXX method if using an AbstractTableModel (I'd be sure to do this in a background thread such as a SwingWorker).
    Last edited by Fubarable; 04-10-2011 at 10:56 PM.

  4. #4
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    If for some reason the GUI doesn't update, then a simple call to jtable.validate() followed by jtable.repaint() will do the trick :)

  5. #5
    hacker9116 is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    i havent tried the above solution yet in that i cannot access my database at the moment i will try but my problem isnt with the database updating its with the gui. as i said above it will update the gui but only if i reload the gui. i was wondering if there is a way that when i click the delete button it will update the jtable with the new database data without me having to reload the gui.

  6. #6
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    And how exactly do you reload the GUI? validate and repaint?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hacker9116 View Post
    i havent tried the above solution yet in that i cannot access my database at the moment i will try but my problem isnt with the database updating its with the gui. as i said above it will update the gui but only if i reload the gui. i was wondering if there is a way that when i click the delete button it will update the jtable with the new database data without me having to reload the gui.
    Click what delete button? Is this delete button in the same code that displays the JTable?

    edit: And revalidate and repaint are not needed if the table model is updated and the correct fireTableXXX method is called if an AbstractTableModel, and even this is not needed if using a model that extends DefaultTableModel.

  8. #8
    hacker9116 is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    yes ok what i have is: i have a gui called delete user it has a Jtextfield, a button(delete), and a JTable that is correctly displaying the users table from my database. i type into the Jtextfield the users username i want to delete, i click delete it successfully deletes that user from the database but the change in the database will not stow in the jtable until i refresh the gui. i was wonder if when a row is deleted it will automatically update the JTable so u can see the change without have to refresh.

  9. #9
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    I ask again, how do you refresh the GUI?

  10. #10
    hacker9116 is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    sorry by "refreshing" i mean closing the window and reopening it

  11. #11
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Closing the window and reopening?!? That just restarts the entire program! Try jtable.validate() and jtable.repaint().

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hacker9116 View Post
    yes ok what i have is: i have a gui called delete user it has a Jtextfield, a button(delete), and a JTable that is correctly displaying the users table from my database. i type into the Jtextfield the users username i want to delete, i click delete it successfully deletes that user from the database but the change in the database will not stow in the jtable until i refresh the gui. i was wonder if when a row is deleted it will automatically update the JTable so u can see the change without have to refresh.
    After deleting from the database, in a background SwingWorker thread you should query the database, and re-populate the JTable's model with data from the ResultSet obtained. Again, if your table's model extends DefaultTableModel, then you're done, but if it extends AbstractTableModel, you'll need to call the fireTableDataChanged method. Again, there is no need to call validate/revalidate/repaint here. Updating the table model and (if it's an AbstractTableModel) calling the appropriate fireTable method will tell the model to update the display.

  13. #13
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    What fireTableDataChanged method?

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ra4king View Post
    What fireTableDataChanged method?
    Please see these links:
    Firing Data Change Events
    AbstractTableModel API

    Again, these methods do not need to be called if the model extends the DefaultTableModel, only if it extends AbstractTableModel.

    Hope this helps.

  15. #15
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Cool, I've never used those before :P

  16. #16
    hacker9116 is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Thanks for all the feedback guys i appreciate it...i will get access to my database 2moro i will update you's on your solutions and again thank you for all your help.

  17. #17
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    > if there is a way that when i click the delete button it will update the jtable with the new database data without me having to reload the gui.

    You are in charge of the code that is invoked when the user clicks on the "Delete" button. Therefore your code needs to do 2 things.

    1) Get the user id from the table and then remove the user from the database
    2) Remove the user from the table using the removeRow(...) method of the DefaultTableModel. There is no need to recreate the entire TableModel. That is an unnecessary inquiry on the database again.

Similar Threads

  1. Ye Olde jTable Refresh Question...
    By Cynot in forum New To Java
    Replies: 4
    Last Post: 05-26-2010, 11:09 PM
  2. JTable vs JTextArea scrolling text question
    By adonos in forum AWT / Swing
    Replies: 2
    Last Post: 05-24-2010, 08:15 PM
  3. Jtable selection question
    By casid in forum New To Java
    Replies: 2
    Last Post: 02-04-2010, 07:11 PM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. Simple question of JTable
    By carl in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 07:07 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •