I have this class for my UI
public class MyFrame extends JFrame{
JTextArea textArea;
public MyFrame(){
setSize(100,100);
textArea = new JTextArea(50,50);
Container content = getContentPane();
content.add(textArea);
}
public static void main(String[] args){
JFrame frame = new MyFrame();
frame.show();
UpdateText u = new UpdateText();
u.settext("Helloworld");
}
}
And I have this another class that will set the text of textArea, in which I extended MyFrame to access textArea in another class.
public class UpdateText extends MyFrame{
public void settext(String msg){
textArea.setText(msg);
}
}
Then I instantiate UpdateText and call the function settext. but the text doesn’t seem to appear in the GUI.
First of all, don’t override the
setText()method unless you want different behavior. Second of all, you don’t have to extend anything. All you have to do is follow these simple steps and you’ll be set!In the
UpdateTextclass, put these lines somewhere in it:In the ‘MyFrame` class, put this line at the beginning:
Now, you can refer to everything in the
MyFrameclass from theUpdateTextclass by preceeding what you want to change withgui. For example, say you wanted to change the text of your textarea. The code would be the following:Happy coding! 🙂