This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Update database by editing the cells in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Update database by editing the cells in JTable" Watch "Update database by editing the cells in JTable" New topic
Author

Update database by editing the cells in JTable

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hello,
I need to update database by editing the values in JTable. i.e., When I change the values in a cell, it must automatically update database. Also when I click insert key, new row must be created and the values inserted to create new data in the database. Please help me in solving this problem.

Regards,
Agatha.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi Agatha

I've just been doing cell editing myself so I'm sure I can help you.

Firstly, how far are you with the table? Are your cells already editable and can you listen for when the user changes information? Does your table load the data or are we going to start the table from the beginning?

And how far are you with your database part?

Cheers,
Rachel
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hai Rachel,

Thanks for coming to my rescue..I am able to get values from the database and populate the Jtable.But I dont know how to proceed from there.
My requirement is that when the user edits one of the cell and moves away from that the changed value should be updated in the database..I dont know how to catch the event..where..in true sense I am stuck here.....

Bye & thanks,
Agatha.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there.

OK, well, let's get you on your way. If you need help at any stage just ask. Sometimes I tend to get ahead of myself when explaining.

Step 1: It's going to be way easier for the whole thing if you make your table extend a TableModel from now (if you haven't already). So if you need help with that just ask.

Step 2: In the table model you need to enable the cells to be editable. In the TableModel class that you make, you need to add these methods


Step 3: You will see in the second method that we fire a method called fireTableCellUpdated. So here we can catch what the use it changing. You need to add a TableModelListener to your table to catch this.

mytable.getModel().addTableModelListener(yourClass);

And in the class that you decide will implement the TableModelListener you need this


So that's the first part of it. Give it a bash and let me know how you get on.

Cheers,
Rachel

[ July 27, 2004: Message edited by: Rachel Swailes ]
[ July 27, 2004: Message edited by: Rachel Swailes ]
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi rachel,

Thanks for your inputs wihtout which I wud still have been stuck.

I am able to listen to the event and get changed values.But I dont want

that the user to move columns through dragging.Is it possible to restrict

the user...from achieving this??

I appreciate the help extended by u...

Thanks & Bye,
Agatha
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi rachel,

Thanks for your inputs without which I wud still have been stuck.

I am able to listen to the event and get changed values.But I dont want

the user to move columns through dragging.Is it possible to restrict

the user...from achieving this??

I appreciate the help extended by u...

Thanks & Bye,
Agatha
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi Agatha

I enjoy explaining code because it help me to make sure that I understand it too.


You can use this code to stop the reordering of columns.

myTable.getTableHeader().setReorderingAllowed(false);

It also has the effect that it disallows column selection.

Cheers,
Rachel
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi Rachel,

You are really helping me a lot. I am not much familiar with swings. I have got stuck with one more requirement. When I click insert key, new row should appear in jtable and values to be inserted in it to store in database. i.e., creating new data from jtable. Can you please help me.

Regards,
Agatha.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
To indert a row into the table, you need a method that will add an element to the Vector or Array that is holding the row information. Add the element to the row and then use the fireTableChanged(null) method so that the table you see will update itself with the new row. From there the user can type in the information they need.

Cheers,
Rachel
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi rachel,

Thanks for the input.I am trying to follow what u have written...I will update u with my results...Will mail u...

Bye & thanks,
Agatha.
gao yun-feng
Greenhorn

Joined: Jul 31, 2004
Posts: 1
hi ,i'm new guy,nice to meet you !
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi Rachel,

I am working on the problem which i said above. I have found the same problem posted by some other person and you have replied it. The solution you gave is a bit helpful to me. Am thankful for your support. I will give my update, immediately i complete my task.

Thanking you,
Agatha.
mahmoud saleh
Greenhorn

Joined: Sep 06, 2008
Posts: 19
iam stuck on the similar problem
and i made a solution but i want to know if it's right

mahmoud saleh
Greenhorn

Joined: Sep 06, 2008
Posts: 19
i tested the code i posted and it worked perfectly
but people on sun forums says that it's such a bad behaviour to mix the ui
and the database,and there's no object oriented here???
and i want to know why this behaviour is bad,i see that it's simple and fast?
please give me some explanation here?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38068
    
  22
Please don't go replying to 4-year old threads.

You want to get the logic for updating the database (in MVC, the "C") a long way away from the display (the "V") because the two perform different things, and it allows easy maintenance if the two are well separated. So that tableChanged method ought to be in a different class; some of the values can be added as fields in the TableModelListener class.
ali honarmand
Ranch Hand

Joined: Oct 12, 2009
Posts: 32

Rachel Swailes wrote:Hi there.

OK, well, let's get you on your way. If you need help at any stage just ask. Sometimes I tend to get ahead of myself when explaining.

Step 1: It's going to be way easier for the whole thing if you make your table extend a TableModel from now (if you haven't already). So if you need help with that just ask.

Step 2: In the table model you need to enable the cells to be editable. In the TableModel class that you make, you need to add these methods


Step 3: You will see in the second method that we fire a method called fireTableCellUpdated. So here we can catch what the use it changing. You need to add a TableModelListener to your table to catch this.

mytable.getModel().addTableModelListener(yourClass);

And in the class that you decide will implement the TableModelListener you need this


So that's the first part of it. Give it a bash and let me know how you get on.

Cheers,
Rachel

[ July 27, 2004: Message edited by: Rachel Swailes ]
[ July 27, 2004: Message edited by: Rachel Swailes ]


Hi Rachel,
I did whatever you mentioned here, but no events is raised. I have got a separate class which implements "AbstractTableModel" and "tableChanged" resides there. I have added "TableModel" and "EventListener" to tableModel as follows:

and My listener class is as follows:


thanks for comments priorly,
Ali


A. Honarmand
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
If the suggestions here didn't work then you probably have a problem with your custom TableModel.

Try using the DefaultTableModel to see if you have a problem.

If you need more help then I suggest creating your own posting since you problem is different than this posting. Don't forget to post your SSCCE in the new posting.
Haris Tasawar
Ranch Hand

Joined: Aug 19, 2011
Posts: 30
hey mahmoud saleh ,
I tried to run your code with some modification according to my database but it gives me ClassLang.exception on this line:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

The error line is :
ps.setString(2, (String) table.getValueAt(row, 2));
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Obviously table.getValueAt(row, 2) returns an Integer, not a String.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Update database by editing the cells in JTable
 
Similar Threads
JTable
JTable(!!!)
JTable
JTable problem
old values from the JTable