This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to detect that the user has typed a character in a JTable column?  RSS feed

 
John-Philippe Verger
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Experts,

I have a JTable with 5 columns, named: "ID", "Name", "UnitPrice", "Qty", and "Total". Only the columns UnitPrice and Qty are editable by the user. When, for example, for a row in the JTable, the user types 5000 in the UnitPrice column, and types 15 in the Qty column, I would want that when he types the first character in the Qty column (i.e. the character 1) in the Qty column, the Total column displays 5000*1, in other terms 5000. And when the user types the second character in the Qty column, i.e the character 5, after having typed 1, the Total column should display 5000*15, in other terms 75000. So, to say it concisely, I would want that the Total column refreshes accordingly each time the user types a character in the Qty column. I have tried to use the MouseClicked event of the JTable, but noticed that that does not solve my problem. Please, is there an event I should use to refresh my Total column? Or should I proceed in another fashion?

Thanks in advance for your suggestions and answers.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not update the value in the Total column until the user finishes editing the cell, because the user could use the Escape key to cancel the editing of the cell. Using this approach you would override the setValueAt(...) method of your TableModel. Then whenever the UnitPrice or Quantity is changed you recalculate the Total.
 
John-Philippe Verger
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob Camick for your reply. In this reply, you've written:
" whenever the UnitPrice or Quantity is changed you recalculate the Total". But my problem is that I don't know how to detect that the user has modified the Qty column (or the UnitPrice column). As I've already written in my first post in this thread, I would want to be able to detect that the user has added a character in the Qty column (or in the UnitPrice column). Please, how could I detect that the user has added a character in the Qty column?
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what happens if:

1. the user enters a character that is not a number? Now you need to add validation code
2. the user enters a couple of numbers and then uses the Escape key to cancel cell editing? How to you reset the value in the Total column?


How to detect that the user has typed a character in a JTable column?"


1. You would need to create a DefaultCellEditor editor and add the editor to your Quantity column of your table.
2. You can then get the JTextField from the editor.
3. Once you have the text field you can add a DocumentListener to the text field.
4. Whenever a character is added or removed the DocumentListener is notified and you can get the text from the text field, convert is to a number and then get the Price from the TableModel and calculate the Total and then update the TableModel to reflect the new Total.

You would need to repeat that above steps for the Price column as well.

Read the Swing Tutorial. There are section on "How to Use Tables" and "How to Write a DocumentListener" that might help.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!