I am making an application in Netbeans and I would like to get the text from the jTextField
and set it to a jLabel
that is in another jFrame
(not the same!)
I did this:
jLabel.setText(jTextField1.getText());
But it doesn't work.
And what event method should I use? actionPerformed
?
Forget about JFrames, forget about JTextFields and JLabels, but instead look at your question in its most basic essence which is:
I want to change the state of one object based on the state of another.
That's it in a nutshell.
This can be easily solved by giving one class a getter/accessor method that extracts the desired information -- here the text in the JTextField, e.g.
public String getFieldText() {
return myTextField.getText();
}
and giving the other class a setter/mutator method that allows outside objects to inject the desired information, here to set the text of its JLabel
public void setLabelText(String text) {
myLabel.setTexzt(text);
}
The devil of course is when to call one or both of these methods, and where, and that will depend on much that you haven't told us, but likely one or both of these methods will be called in event code, such as in an ActionListener's actionPerformed method.