This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Update database by editing the cells in JTable

 
A Kumar
Ranch Hand
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,i'm new guy,nice to meet you !
 
A Kumar
Ranch Hand
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iam stuck on the similar problem
and i made a solution but i want to know if it's right

 
mahmoud saleh
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
Java Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Camick
Ranch Hand
Posts: 2475
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously table.getValueAt(row, 2) returns an Integer, not a String.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic