WHat i want to do is, make a Jtable editable and then when the user makes some changes within the cell and click update button, i want to save the new updated data in my database. I know i have to use getSelectedRow() and get row number and then use getValue to get the information and then save it..
But the setSelectedRow is always returning -1, i havent programmed the other bits yet as i want it to display just the row number 1st. I am using Java beans and i m really struggling. Heres my SSCE.
Actually this is not a true SSCCE since it has outside dependencies that are not available to all and are not required to demonstrate the problem: the database code. Also I don't see any place where you are using a TableModel, and I believe that this will be required for you to have an editable JTable.
Joined: Jan 03, 2010
my jtable is already editable because of isCellEditable(int row,int column). As for the table model i believe the table automatically creates this for you..
ok i understand the ssce bit but this was the best i cud do without changing the whole cod..i am more then happy to email the database across or for someone to access my pc using team viewer if they please to..i just want to get this sorted..been playing around with this for so long now and still not close.
ok i understand the ssce bit but this was the best i cud do without changing the whole cod..i m more then happy to email the database across or for some1 to access my pc using team viewer if they please to..i just want to get this sorted..been playin around with this for so long now and still not close.
The reason you are spending so much time is because you "haven't created a SSCCE".
The database is completely irrelevant to your question.
For your SSCCE all you need to do is create a JFrame with a JTable and a JButton. Then in the ActionListener you add to the button you get the selected row of the table. It should take about 10-15 lines of code to test this. Once you understand how to do this then you add the code to your real program because you've proven the simple concept works.
i take it you are mr camikr on sun forums..well ok i have created the SSCCE like you said..just a frame, table and button. it still returns -1 when you click button to get the row number. would you like to see this sscce?
No i am not trying to waste anyone's time..i know hw important it is to people..but we all make mistakes n i did as well.
When you press the button there may not be a selected row.
I see two options:
- store all changes, then propagate these when pressing the button. The TableModelListener is notified upon changes so you can store only these. After the update you clear the changes.
- simply update everything
There is also an intermediate solution which doesn't need TableModelListener.
- Use a custom TableModel (override DefaultTableModel for the easy way) with an extra Boolean column called "updated".
- Override setValueAt to change this column's value if any other column's value is changed.
- In the JTable get a reference to the matching TableColumn, then remove that from the JTable so it will not be visible. You can use the TableColumnListener for this.
When you then press the button, perform this pseudo code:
That is the point of creating a SSCCE. If it works, great. If it doesn't work, then you have simple code to post on the forums so we can see what you have done.
Joined: Jun 13, 2009
No i am not trying to waste anyone's time..
But this is what has happened.
In the other posting you where given two possible solutions depending on your requirements:
a) use a TableModelListener if you want to update the database after every cell has changed
b) use a button to update a single row after all the data on the row has been changed.
People in this posting are not aware of these suggestions and have repeated them. Therefore it has been wasted time.
And that is the problem when you post the question twice. People don't know what has already been suggested.
Joined: Jan 03, 2010
code for bean file.
Hope this is exactly what you are looking for.
Joined: Jan 03, 2010
Rob i have the SSCCE working and displaying the row number correctly..but when i am implmenting this into other SSCCE which is using java beans, it always returns -1. However it picks up the data within the table but it doesnt show the selectedrow for some reason. Can you help with this?