Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: How to get the new value from the cell of the JTable ?

 
kwwong wong
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to get the new value from the cell of the JTable ?
When I editing the cell in JTable, i.e. change the owner field, and then click the "book" button to save the record, it cannot retrieve the new value of the owner. When I use the getValueAt method it gives me the old value! It seems that the JTable do not trigger the setValueAt method.
However, when I left the row I am editing to another row (at this time the JTable trigger the setValueAt method) and then reselect the editing row. At this time, when I click the "book" button, the new value is saved to the database.
How to solve this problem ?
 
Mike Southgate
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your setValueAt mehtod, are you updating both the table model and the database? Only doing one or the other might cause what you describe.
ms
 
Mike Southgate
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One question is why do you have a book button. I just let the JTable handle the update of both the database and the table model for me. Now that I think about it, having a book button forces you to do the database update outside of your table model. I'd just verify that your setValueAt method is being called (just use System.out.println if you don't have a debugger) and make sure it updates the table model properly (use getValueAt).
ms
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless they are requiring you to have th changes be made in the JTable directly. I think the better approach is to have the JTable be read-only. And have a JTextField and a Book JButton just below it to allow the user to enter the data. Obviously the data entered will be in correlation with the row that is selected in the JTable.
This was my approach, I received 100% on GUI, so I know that I didn't lose points for that design.
As with everything in the assignment, your design is completely your decision. So you can choose how the user books any way you want. At least make sure that it is user friendly/ follows basic GUI standards and operates correctly.
Mark
 
kwwong wong
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found that the setValueAt is not trigger in JTable if I have not made the cursor leaving from current editing cell to another cell in JTable. So I cannot retrieve the new owner when I click the book button if the cursor is on the record's "owner" cell.
I have followed Mark Spritzler and it works fine. Thanks.
 
Davidd Smith
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi mark,
You did well on GUI, I hava a guestion.
I also make the Table read only and I put a input panel with a find button on top of it for user to type in find criteria. My input panel contains every field of the record so that user can find based on whatever combination of conditions. My question is: Should I get the fields from the Data class to build my JLabels and JTextFields and tableHeader or I only hard code it in the GUI? In the former case, I can only use the JTextField and can not provide JComboBox or JCheckBox which will make the GUI friendly and reduce input error, meanwhile, the JLabel and JtableHeader's content must be the fields' name which may be not clear. In the former case, if the schema of database is changed, we must re-write the GUI, so the mantainability is bad.
What is your suggest? I am looking forward it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic