how to set new text in JTextField after creation?

2019-02-27 17:30发布

问题:

I have a jTextField , and I set it's value to a certain sum when I create the frame.
Here is the initiation code:

totalTextField.setText(
            itemsPriceTextField.getText() +           
                    Float.toString(orderDetails.delivery)
);

This textfield should show a sum of items selected by the user.
The selection is done on a different frame, and both frames are visible / invisible at a time.
The user can go back and forth and add / remove items.

Now, every time i set this frame visible again, I need to reload the value set to that field
(maybe no changes were made, but if so, I need to set the new correct sum) .

I'm quite desperate with it.
Can anyone please give me a clue?
Thanks in advance! :)

回答1:

first, thanks to all helpers, appreciate this..

the solution is quite simple actually:
before setting the frame visible again, one should update the fields with the new values / states.
something like:

jTextField.setText("put your text here");  
jRadioButton.setSelected(!isSelected());  
.  
/* update all you need */
.  
jFrame.setVisible(true);

the frame will come up with the new values / states.

simple enough, just never tried to update at this point.



回答2:

Add a WindowListener to the frame. Then you can handle the windowActivated event and reset the text of the text field.

See How to Write Window Listeners.