I want to be able to change the content of a some JLabel objects from another class. I have a GUI class containing all GUI objects (JFrame, JPanel, etc.). Let's say I have only one JLabel:
public class GUI {
private JLabel label;
public GUI() {
initialize();//initializes all objects
}
public void update(String s) {
this.label.setText(s);
}
}
GUI has a public function update(String) that I hope to call from another class.
My main class is called App:
public class App {
private static GUI window;
public static void main( String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
window = new GUI();
App.updateTxt("some string");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void updateTxt(String s) {
window.update(s);
}
}
This solution does not work, however, GUI.update(String) is working perfectly when called inside the GUI class.
I checked the solution proposed by Paul in:
Access GUI components from another class but I did not understand it.
So how can I invoke a method on GUI from another class to change the UI?