I'm making a postal service application. I'm getting the weight of the box in one text field and I want to show it's price at the moment in another text field which is not editable. for example if he typed 100, then that text field text sets to 1000 and if he typed 200, the the text field sets to 2000 with no need of pushing any button etc. Thanks in advance
4 Answers
Add a KeyListener to the text field that would update the price whenever a new value was entered. You might want to add a delay that would only update the price after a second or two to make sure the user finished entering the value instead of updating with each key stroke.
http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

- 23,473
- 9
- 54
- 76
-
AWT might use a KeyListener because there was no other choice. Newer APIs have evolved of the last decade. Swing was designed to take advantage of these API's. As a rule it is better to use a more abstract API when it is available instead of a low level API tied directly to an low level event. – camickr Nov 04 '13 at 15:52
Add a DocumentListener to the Document of the JTextField. It will be invoked when text is added/removed from the text field.
Read the section from the Swing tutorial on How to Write a Document Listener for more information.

- 321,443
- 19
- 166
- 288
-
This is definitely the right answer. Using a KeyListener for this is madness. – Cruncher Nov 04 '13 at 15:54
you need add an event listener for the onchange event to your text input and when the event is received, perform the calculation and update the second text input
The following topic shows how to add the event listener Value Change Listener to JTextField

- 1
- 1

- 1,200
- 2
- 16
- 30
set up an a keylistener to listen for inputs in your weight textField,
set up a switch statement to convert the weightTextField's input to the pricePerWeight textfield and pricePerWeight.setText()

- 371
- 1
- 7
- 19
-
-1, this was suggested earlier (even though it is a bad suggestion). No need to clutter the forum with duplicate suggestions. – camickr Nov 04 '13 at 16:30