• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Help with JTable

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.

1st the main code.



Now the bean code



Your help is greatly appreciated.

Thank You
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Attach a TableModelListener to your TableModel instead. That way you can listen to tableChanged events where the event's getType() returns TableEvent.UPDATE.
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Attach a TableModelListener to your TableModel instead. That way you can listen to tableChanged events where the event's getType() returns TableEvent.UPDATE.



is it not possible to do it this way? I wanna try and do it this way..it might not be best way but i wanna get it working this way as well.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

And quit cross posting: http://forums.sun.com/thread.jspa?threadID=5422135&tstart=0. You got the exact same advice in your other posting. So Instead of wasting the time of 2 people you've now wasted the time of 4 people by not listening to the original advice.
 
Rob Spoor
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mayur Gosalia wrote:As for the table model i believe the table automatically creates this for you..


Correct, it creates a DefaultTableModel for you if you don't provide one yourself.

cud... some1... playin


http://faq.javaranch.com/java/UseRealWords
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Rob Spoor
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

would you like to see this sscce?



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.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


code for bean file.



Hope this is exactly what you are looking for.
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to get the values(column values) of selected row in Jtable
 
Rob Spoor
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't hijack threads. Create your own thread instead.
 
reply
    Bookmark Topic Watch Topic
  • New Topic