I have a method called getTheUserInput in a class which returns a String which updates based on user actions, which is located inside a class called Board.java. I have action which adds data to a String which I know works.
In my other class I have a JLabel which has been set to equal the returned String of the method, as follows:
JLabel l = new JLabel(b.getTheUserInput());
However, when I run the application no matter what input the user provides, the string updates, but the JLabel stays blank. How do I keep the JLabel updated consistently with the string in the other class?
You’d …. need to update the
JLabel.Strings in Java are immutable so they can never change / be changed.For example when you say, “I have action which adds data to a String which I know works.” … you’re incorrect. You’ve created a new
String. You need to supply theJLabelwith the newString(the user’s input) if you want to change the text.Edit: To answer the last part of your question; you’d need to keep track of the
JLabeland update it as I show above every time the user provides input (in the event handler for whatever that is). Using the Observer Pattern might be an option as Java provides it viaObserverandObservable