• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Update database by editing the cells in JTable

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have created a form in which i added a jtable which i guess is of type defaulttable model

it was created by the GUI builder. I added a button to get information from the database and display it on this jtable and it does that ok. The problem now is that i need to update , insert and delete values of the database through the jtable and not through individual text box fields. I am very new to java and i need to have this done by end of today and i dont know how to do this? i can delete and update from individual text fields but how does one do this through the jtable and table model. I know that the jtable is merely for viewing purposes and the tablemodel is what actually holds the data and somehow i need to add methods to add a vector (of type that the columsn are made up of) to the table model and eventually have some SQL statement that actually inserts this new vector(row) into the db? i am lost though

here is the code of how i am getting info from the db and presenting it to the jtable for viewing

 
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You appear to know that it's the table model which will have to do this work, but still you didn't post the code for the table model.

Anyway. You would have to override the addRow() and insertRow() methods to handle inserts to the table, and you would have to override the setValueAt() method to handle updates to existing rows. And removeRow() to handle deletes.
 
Paul Agola
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You appear to know that it's the table model which will have to do this work, but still you didn't post the code for the table model.

Anyway. You would have to override the addRow() and insertRow() methods to handle inserts to the table, and you would have to override the setValueAt() method to handle updates to existing rows. And removeRow() to handle deletes.



you ask for the table model code but i stated that this has been created by netbeans? what exact code would you be reffereing to. I used netbeans and imported the default table model package so i dont udnerstand what code you need me to post? I have never developed java swing components from scratch and i am using netbeans which of course isnt recommended but that is what has been chosen. These methods that i have to override are they part of the table model class and if so how do i do that?
 
Paul Agola
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You appear to know that it's the table model which will have to do this work, but still you didn't post the code for the table model.

Anyway. You would have to override the addRow() and insertRow() methods to handle inserts to the table, and you would have to override the setValueAt() method to handle updates to existing rows. And removeRow() to handle deletes.



logically i know i have to write code to perform these steps

1. add listener to jtable component
2. get row and column selected
3. Addrows and column selected to model
4. Add vector to the database via a select statement

but sinceim new to java swing (and java itself) and i created my gui with netbeans this is a bit hard for me to do
 
Paul Clapham
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I see. So in fact you haven't written a table model class. Then you will have to do that, I suppose. Or perhaps you could attach a TableModelListener to the table model and update the database based on the TableModelEvents you receive from it.

Although I am completely unfamiliar with Netbeans -- for all I know it might have a database-backed JTable as one of its choices.
 
Paul Agola
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Ah, I see. So in fact you haven't written a table model class. Then you will have to do that, I suppose. Or perhaps you could attach a TableModelListener to the table model and update the database based on the TableModelEvents you receive from it.

Although I am completely unfamiliar with Netbeans -- for all I know it might have a database-backed JTable as one of its choices.



im thinking the same thing. With the IDE i think i will be able to listen for events by altering the properties of the Jtable control. But as for the methods it already contains them and i am lost onto how to add rows from thejtable and have that propagate to the database
 
Paul Clapham
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if it were me I would just write code to do that. But you seem to be limiting yourself to what Netbeans can do for you? If that's the case, then just say so and we can move this post over to the IDEs forum where you're more likely to get Netbeans answers. (I posted a Swing answer because this is the Swing forum.)
 
Paul Agola
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i am going to take it one step at a time. I figured to isnert rows the first thing i will have to do is insert a new vector in the tablemodel like so



where the new Object is the vecotr consisting of the column names that will be added. But when i press the button it is not doing anything. Im not limiting myself i just wantedot know wehre to start writing the code.
 
Paul Clapham
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you know it is "not doing anything"? Or to put it another way, what exactly were you expecting it to do, what did it do, and what's the difference between the two?
 
Paul Agola
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new developments. I have added code to be able to
1.insert an empty row in the jtable and
2. delete an empty row in the jtable

as you can see this is not very practical here is the code used for each button. Myabe now i have shown that i am trying to actually code lol

code to add empty row


code to delete empty row



i know for the add i have to add some values but im not sure how to do it.
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In the first post of your code, I see that there are 4 columns in the db table, but you seem to display 7 columns in the JTable:



So, when you are trying to add data or insert, you should pass 7 values to the Object array.

Your code for deleting the row by getting the selected row is ok. But, this removes data only from the table and hence from the view. You still need to write code to delete the row from the db. For this, you would need to find your 'where' clause and pass appropriate value to it. You can get the value of the columns via model.getValueAt(row, col) call.
reply
    Bookmark Topic Watch Topic
  • New Topic