Results 1 to 17 of 17
Thread: JTable Question?
- 04-10-2011, 10:38 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
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.
- 04-10-2011, 10:43 PM #2
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.
- 04-10-2011, 10:48 PM #4
If for some reason the GUI doesn't update, then a simple call to jtable.validate() followed by jtable.repaint() will do the trick :)
- 04-10-2011, 10:55 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
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.
- 04-10-2011, 10:56 PM #6
-
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.
- 04-10-2011, 11:06 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
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.
- 04-10-2011, 11:09 PM #9
- 04-10-2011, 11:11 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
sorry by "refreshing" i mean closing the window and reopening it
- 04-10-2011, 11:14 PM #11
Closing the window and reopening?!? That just restarts the entire program! Try jtable.validate() and jtable.repaint().
-
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.
- 04-10-2011, 11:18 PM #13
-
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.
- 04-10-2011, 11:24 PM #15
- 04-10-2011, 11:25 PM #16
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
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.
- 04-11-2011, 12:43 AM #17
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
> 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
-
Ye Olde jTable Refresh Question...
By Cynot in forum New To JavaReplies: 4Last Post: 05-26-2010, 11:09 PM -
JTable vs JTextArea scrolling text question
By adonos in forum AWT / SwingReplies: 2Last Post: 05-24-2010, 08:15 PM -
Jtable selection question
By casid in forum New To JavaReplies: 2Last Post: 02-04-2010, 07:11 PM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM -
Simple question of JTable
By carl in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 07:07 AM


LinkBack URL
About LinkBacks


Bookmarks