• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding new row to JTable via form during run time

 
Alex Lai
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm trying to make an application that stores, delete and view customer data.
As the subjects says, is there a way where I could enter data into a form and see it in the JTable, possibly as a new row to the table.
Such as, I have a JFrame object that opens a JDialog box. On the JDialog box there are two buttons to choose from, one that opens a form (on a JDialog) to enter new customer information and the other that opens a JDialog and shows a table of customers (using a JTable and my own implemented table model).

This is the code that I have for the customer table (including the Customer class that stores the data and my table model):


I've tried various tutorials (including Sun's) but didn't get much out of it.

Any ideas on how I could approach this?

Help would be greatly appreciated.

Alex.
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you understand the code you posted, you can see there is an addCustomer(Customer c) method in the CustomerTableModel class. What you need to do is to gather data from user in the EditCustomerDetailsDialogue class and create Customer object form that data. Then all you need to do is call the addCustomer method and pass that customer object. This smells like a home work assignment so I will not go into too much detail. If you have specific questions feel free to ask.
 
Alex Lai
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply,

so to clarify, each time an instance of JDialog that contains a form is opened, I would construct a Customer object and get the data from the data fields and assign them to the instance variables of the newly constructed Customer object? Then using this object as a parameter and pass it to the addCustomer(Customer c) method?

Here's a snippet of my 'to-be' code for the form on adding a new customer:


Would this be a valid and reasonable way to do it?

*EDIT* Okay, I know the question just asked sounds stupid. As a Java learning programmer I should test it out and not just rely on others experiences.
I'll test it out and post a result.

Alex
 
Alex Lai
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried and have implemented the 'to do' code as posted earlier. It works!

Now, I'll have to go and firgure out how to edit the data... I'll probably write a method getCustomerAt() and override the setValueAt() method and then use one of the fire..() methods to update the table.

Thank you for the help. Really appreciated it.

Alex
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!