how to set new text in JTextField after creation?

和自甴很熟 提交于 2019-12-02 09:42:41

问题


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:


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.




回答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.



来源:https://stackoverflow.com/questions/6350797/how-to-set-new-text-in-jtextfield-after-creation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!