• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sharing document between JTable Cells and JTextFields

 
Ste Graham
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - in my GUI I have a JTable which lists the records in a database. Below the JTable there is a group of JTextFields which act as a "form" for individual database records. So, for example, you click on a row within the JTable and the individual record is displayed within the JTextFields.

The problem I am having is getting the record in the JTextField format and the (same) record in the JTable row to update when one of the ways is used for updating. I think I have two ways to handle this:

1. The users decided which way they want to update the record (either using the JTable or the JTextFields), and when they hit the "Update Record" button the databses is updated, the TableModel is updated to reflect the change and the new TableModel is now used to show the data in the JTable and the JTextFields.

2. I share the underlying document between the JTextFields and the Cells in the JTable, so when I type in the JTextField the data is automatically typed in the cell and vice-versa.

I've tried both ways, but without much success. The problem I have with the first one is, if the "Update" button is the same for both JTextFields and JTable (which it is) how do I know which value I should use to update the databases with - the JTable value or the JTextField one?

For the second way, I'm unsure the best way to go about it. Do I need to create new DefaultCellEditor objects with JTextField objects, so I then can use the getDocument/setDocument methods? I tried something like:



This doesn't see to work as expected and only works one way when it does work.

As anybody expereinced this before? Any soultions, tips etc.

Cheers, Stephen
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic