I have a jtable with column item,quantity,rate and amount.If i enter value in quantity and press tab key the total amount is getting calculated that is working fine.But i need the amount to be calculated on typing the quantity. I want it to be done on key pressed of numbers or on typing number.I have used default jtable using netbeans
Asked
Active
Viewed 1.1k times
0
-
So, you it so that as the user types, the `amount` is calculated in real time? – MadProgrammer Aug 16 '13 at 08:07
-
yes,on billing when particular items quantity is entered the amount must be calculated. – user2436012 Aug 16 '13 at 08:09
-
But why the header says get cell values of jtable? – Gerret Aug 16 '13 at 08:10
-
Forgive me, but why is it important that this is done in real time? – MadProgrammer Aug 16 '13 at 08:11
-
You need to understand how the `TableCellEditor` works. You need to add an `ActionListener` to the `TableCellEditor` component and then you can do whatever you want. – Boris the Spider Aug 16 '13 at 08:13
-
2The issue is, the editor shouldn't be changing the state of the table, this leads you down a slippery slope of problems, hacks and work arounds... – MadProgrammer Aug 16 '13 at 08:15
-
1Check out this answer: http://stackoverflow.com/a/14540685/44522 (it's quite a duplicate question) – MicSim Aug 16 '13 at 08:17
2 Answers
3
to get cell value you can to do something like that
table.getModel().getValueAt(rowIndex, columnIndex)

Sergii Zagriichuk
- 5,389
- 5
- 28
- 45
-
Yes, because there are two layers top is table(model) low is text editor(cell), the value is present on lovest layer and you can reach it using TableCellEditor (as described above), after "flushing" data, value will be present in table model (top layer) and can be reached by my exaple. – Sergii Zagriichuk Aug 16 '13 at 11:02
3
In response to an answer suggesting getValueAt()
, you say, "I used the above code but it prints as null
." As discussed here, the value is not available in the model until the editor concludes. You'll need a custom TableCellEditor
that uses a DocumentListener
to update your total.