Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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 ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Sharing document between JTable Cells and JTextFields

Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic