This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable & Edit Button Click - Plz Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable & Edit Button Click - Plz Help" Watch "JTable & Edit Button Click - Plz Help" New topic

JTable & Edit Button Click - Plz Help

Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

Dear All

I am using JTable. I need suggestion regarding following. Please help me out.

I need suggesgtion that at background what should i use to know that a particular row is added,edit,delete or a certain operation is done.

All the following operation are done after clicking Edit Button.

1. I have add button to add row into jTable.

2. I have edit button to edit a particular row. And there is another column that shows that if "YES" is selected then only "YES" option value are updated. Is this correct.....???

3. I have delete button to remove a particular row. How do i know if 3 rows are deleted randomly out of 10 rows. (Shoud i use vector or something else)

4. What if Row # 3 is deleted and another Row is added at last. How do i know that which row is added as total rows are 10 in both cases (delete/add row).

5. How do i know that particular row is edited. Let column 1 has code = 1001 and that column value is now change to 1002. (I think i can get this from YES option as i defined in Q#2.) Is this correct.

Please help me what technique should i use to handle my all required events.
Answer me if you can help me out regarding any of above scneario.

Thanks in advance.

Love is GOD and GOD is Love.
Anand Karia
IT Concretor.......
M/s. Anand Karia Concreting IT
Petr Blahos
Ranch Hand

Joined: Apr 28, 2004
Posts: 131

This is rather tough. Firstly I can't say I understand your question, especially the second paragraph. I can try giving you a few of tips though:

1) Read Swing tutorial for JTables (google for java swing jtable tutorial).
2) Implement a proper table model.
3) Implement a table model listener. (There are sections discussing these issues in the tutorial)
4) Post questions concerning Swing to Swing forum.
5) Remember that MVC has a lerning curve.

Best regards,

Get a better web browser:<br /><a href="" target="_blank" rel="nofollow"></a>
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Assumption: You are presenting a bunch of data rows in the table, letting the user add/edit/delete them at will, and then saving all the changes at once (e.g. with a save button).

Given this pattern, you'll want to create a table model that tracks the changes the user makes for saving later. One method I've found useful is to maintain two extra collections along with the basic model: addedItems and removedItems.

When the user edits a row, either set up the row items to track this with a boolean or add a third collection to the model: editedItems.

Then, as the user makes changes, add the row items to the appropriate collection. Make sure the collection doesn't allow duplicate rows (you can't delete the same item twice).

When it's time to save the changes, either bundle them all up to send elsewhere (to an EJB or remote service or whatever) or apply the changes directly, collection-by-collection, row-by-row. Off the top of my head, in the past I'd delete items, then update items, then add items. This order allows the most likely combinations to occur naturally.

For example, starting with rows A, B and C, the user can delete row B, change row C to B, and add a new row C without problems. Of course, this can vary depending on your method of saving and the standard use cases.

As for the JTable (the view), it listens for various table-modification messages from your model. Therefore, your code (the controller) that responds to add/delete button presses and cell updates should operate on the model itself, and the table will repaint appropriately.
[ November 04, 2004: Message edited by: David Harkness ]
Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

I'm going to move this to the Swing forum where I think its a bettter fit.

Please post your replies there, thanks!
I agree. Here's the link:
subject: JTable & Edit Button Click - Plz Help
It's not a secret anymore!