• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic